I have a simple HTML page which calls a JavaScript function:
<!DOCTYPE html>
<html>
<script type="text/javascript" src="css/myscript.js"></script>
<head>
<link rel="stylesheet" href="css/style.css">
</head>
<body onload="javascript:hello()">
</body>
</html>
This is my hello() function:
<script>
function hello() {
alert("load new content");
document.open();
document.write("<h1>Changed content!</h1>");
document.close();
}
</script>
When pasting the JavaScript block into the page directly, it works. However it's not able to find hello() if the JavaScript is contained within a dedicated file.
Using the Developer tools I can see that the JavaScript file is loaded successfully.
css/myscript.jsis the right path?<script>isn't allowed directly in the<html>tag. It needs to be in either the<head>or the<body>.