Help with error SCRIPT5007: Unable to set value of 'onclick' property

I have a link on an html page and it has id="redirect"

and this is in (script.js)

window.onload = initAll();

function initAll(){
    document.getElementById("redirect").onclick = clickHandler;
}
function clickHandler(){
    alert("Sure!");
    return true;
} 

I get an error

SCRIPT5007: Unable to set value of the property 'onclick': object is null or undefined 

I tried this in (IE9, IE8, chrome, Firefox 4.0.1) and still not working, please help

+3
source share
1 answer

You need to write:

window.onload = initAll;

At the time of execution, the document element does not exist, because you are not calling the function at loading. You call initAll()and assign its return value window.onload, which is undefined or an error message.

+4
source

All Articles