Question

<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

Was it helpful?

Solution 2

Try this

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

The issue is with nested quotes

Here is a working demo

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top