【问题标题】:Uncaught SyntaxError: Unexpected token when using localhostUncaught SyntaxError: Unexpected token when using localhost
【发布时间】:2015-10-13 06:27:45
【问题描述】:

我是 javascript 新手,需要帮助。 我创建了一个 index.html 和一个 app.js(都在同一路径中),并将 index.html 托管为 localhost。

当我运行单个 index.html 时,我确实收到了 hello 警报,但是当我使用 localhost 运行时,我收到“Uncaught SyntaxError: Unexpected token

下面是我使用的代码:

索引.html:

<!DOCTYPE HTML>
<html>
    <head>
        <title>Show Javascript alert</title>
        <script type="text/javascript" src="app.js"></script>
    </head>
    <body>
    </body>
</html>

app.js:

function display_alert(){
    alert("Hello!");
}
window.onload = display_alert;

【问题讨论】:

  • 首先,我会将您的window.onload = display_alert; 更改为window.onload = display_alert();,查看here 了解更多信息。
  • 我在本地主机上工作。好像你的 app.js 有无敌或肮脏的符号。这是原始文件:qush.it/nrcW
  • 错误信息似乎与问题中的代码无关。
  • 可能是因为 chrome 将
  • @GrantWeiss 当您使用window.onload = display_alert(); 时,会立即调用函数display_alert。当您像在原始帖子中一样使用它时,当onload 事件发生时,该功能就会被执行。这似乎是理想的行为。

标签: javascript html localhost


【解决方案1】:

我认为网络服务器没有为您的 app.js 文件提供服务。您是否尝试在浏览器中打开它,检查路径是否正确?(例如http://localhost/app.js)。

检查文件的名称是否也是小写,以及网络服务器是否已读取文件的权限。

【讨论】:

  • 嗨 Torben,当我打开“localhost:8080/app.js”时,它会打开 index.html。可能是什么原因?
  • 您使用的是哪个网络服务器?您的服务器的操作系统是什么?
  • 我在 Windows 7 Enterprise SP1 上使用 node.js 网络服务器。
  • 这应该是 node.js 脚本中 HTTP-Route 定义中的错误。我必须查看您的 node.js 代码才能确定问题
  • Node.js 本身并不是一个 Web 服务器。您必须有某种脚本才能通过 http 使其成为服务器文件。您使用哪个命令来启动服务器?
猜你喜欢
  • 1970-01-01
  • 2013-02-25
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 2012-05-17
  • 2019-02-10
  • 2014-07-08
  • 2011-03-09
相关资源
最近更新 更多