最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Alert function not working in Body tag - Stack Overflow

programmeradmin2浏览0评论
<body onload="alert("Hello World")">
<h1>Hello World!</h1>
</body>

why doesn't this work?

EDIT Additionally, why does chrome dev tools report "Uncaught SyntaxError: Unexpected token } " that as the error message?

Thanks, funbeans

<body onload="alert("Hello World")">
<h1>Hello World!</h1>
</body>

why doesn't this work?

EDIT Additionally, why does chrome dev tools report "Uncaught SyntaxError: Unexpected token } " that as the error message?

Thanks, funbeans

Share Improve this question edited Nov 4, 2013 at 20:38 Dave2081 asked Nov 4, 2013 at 19:42 Dave2081Dave2081 3392 gold badges6 silver badges16 bronze badges 2
  • 3 Next time when something doesn't work, before wondering why, press F12. – dfsq Commented Nov 4, 2013 at 19:45
  • Actually, interesting point, and I wish I would've put this in the question. Chrome just keeps saying there was an unexpected character "{" and I kept looking for a curly brace, but there were none. – Dave2081 Commented Nov 4, 2013 at 20:37
Add a ment  | 

2 Answers 2

Reset to default 5

The issue is with double quotes, you need to escape them:

<body onload="alert(\"Hello World\")">

Or use single quotes:

<body onload="alert('Hello World')">

This happens because when you use double quote HTML think that your attribute is ended and contains of only alert( and the rest "Hello World")" is another attribute.

Try this

<body onload="alert('Hello World')">
<h1>Hello World!</h1>
</body>

The issue is with nested quotes

Here is a working demo

发布评论

评论列表(0)

  1. 暂无评论