0

I want to make something like library in JS. If table has a class="aClass" I want to automatically modify some of it's elements using JS.

I tried this way:

window.onLoad = LoadSetup();

function LoadSetup()
{
  tables = document.getElementsByTagName("table");
  alert(tables.length);  // it's 0 though there are is one in the document
}

But i seems that this function runs before loading html. How to fix this?

4 Answers 4

1

Two problems:

  1. onload needs to be all lower case. Rather than binding an event handler to window.onload, you are adding a custom property to window of onLoad.
  2. Assign onload a reference to the function. Omit the parentheses. With the parens, you are calling the function immediately, and assigning its return value to window.onLoad.

Try this:

window.onload = LoadSetup;
Sign up to request clarification or add additional context in comments.

1 Comment

You are right, thanks. It was hard to track for me (2 days of experience with JS :) ) as it actually did some action more or less what I expected and when I expected. Thanks one more time.
1

Why not to use:

jQuery(document).ready(function() {
LoadSetup();
});

from JQuery: http://jquery.com/

Regards,

1 Comment

jQuery is a tool that is great when it is needed. Plain old javascript will more than do the trick here..
1

Try

window.onload = LoadSetup;

Including the parenthesis will execute the function and set window.onload to the return value of the function.

Comments

0

You're assigning the result of the function LoadSetup. Assign the reference LoadSetup instead:

window.onload = LoadSetup;

Also note it's window.onload (case sensitive).

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.