My javascript and html files named contents.js and page.html:
function sayHello() {
alert("Hello World")
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script type="text/javascript" src="contents.js" ></script>
<script>
window.onload = sayHello();
</script>
</body>
</html>
When run the sayHello function isn't being called. In firefox's console it returns the errors:
-SyntaxError: expected expression, got '<' contents.js:1
ReferenceError: sayHello is not defined
Both files are saved in the same folder. And i'm using a node.js express project in eclipse to create the server:
var http = require('http'),
fs = require('fs');
fs.readFile('./page.html', function (err, html) {
if (err) {
throw err;
}
http.createServer(function(request, response) {
response.writeHeader(200, {"Content-Type": "text/html"});
response.write(html);
response.end();
}).listen(8888);
});
Why am I not able to call the 'sayHello' function in the contents.js file from the page.html file?
window.onload = sayHello();assigns the return value fromsayHellotowindow.onload. What do you return?onloadoronpageshow.