Calling function in Javascript

Creating a function in javascript is very simple but calling / invoking the function sometime gets tricky when a function don’t have a parameters, and wondering why the function is it not working….

Example:

  1. function countBodyChildren() {
  2.  var body_element = document.getElementsByTagName("body")[0];
  3.  
  4.  alert(body_element.childNodes.length);
  5. }

and you invoke the function like this

  1. window.onload = countBodyChildren();

when you run your program, nothing happens. So, what’s the problem?
In javascript, calling function without parameter should not have an open and close parenthesis, that’s all folk….:)

It should be

  1. window.onload = countBodyChildren;

,

  1. No comments yet.
(will not be published)
Submit Comment

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Subscribe to comments feed
  1. No trackbacks yet.

SetPageWidth