<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
2 Answers
Reset to default 5The 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