【发布时间】: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