0

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?

2
  • 2
    window.onload = sayHello(); assigns the return value from sayHello to window.onload. What do you return? Commented Aug 4, 2016 at 16:23
  • You forgot to use a trigger event. Like onload or onpageshow. Commented Aug 4, 2016 at 16:26

3 Answers 3

2

The code

window.onload = sayHello();

...calls sayHello and assigns its return value to window.onload, exactly the way x = foo() calls foo and assigns its return value to x.

To just assign the function reference, don't call it (remove the ()):

window.onload = sayHello;

Side note: The window load event happens very late in the page load cycle, only after all other resources are loaded, including all images. Most of the time, you want to run your code earlier. If so, just put it in a script tag that you include at the end of the HTML, just before the closing </body> tag.

Sign up to request clarification or add additional context in comments.

1 Comment

To expand, sayHello() returns undefined so window.onload is being set to undefined which is why nothing happens.
1

Executing sayHello() , you arenot returning anystuff so it was not executed. Had sayHello been returning a function it would have invoked that onload.

var sayHello = (function() {
  return function {
    alert("Hello World")
  };
})();

In this situation, one might use sayHello(), as its returning a function.

window.onload = sayHello;

function sayHello() {
  alert("Hello World")
}
<!DOCTYPE html>
<html>

<head>
</head>

<body>
  <script type="text/javascript" src="contents.js"></script>

</body>


</html>

8 Comments

Please add an explanation. Just code doesn't help anyone.
Hey @AndrewL. Thanks for pointing it out, I just about to update it. I hope you would reconsider your opinion. Will be catious next time bro!
I tried this and the reference error is now gone but im still getting: SyntaxError: expected expression, got '<' contents.js:1 and the alert is not appearing.
Hey @user2562530 Can you just run the snippet I shared, it has the alert(). just run it once.
Okay it works here but when I copy it to eclipse i still get the syntax error. What can I do?
|
-1

Put it inside the body tag like this.

<body onload="sayHello()"></body>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.