javascript - help with error SCRIPT5007: Unable to set value of the property 'onclick' - Stack Overflow

I have a link in the html page and it has the id="redirect"and this in (script.js)window.onlo

I have a link in the html page and it has the id="redirect"

and this in (script.js)

window.onload = initAll();

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

I'm getting this error message

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 running, please help

I have a link in the html page and it has the id="redirect"

and this in (script.js)

window.onload = initAll();

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

I'm getting this error message

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 running, please help

Share Improve this question edited Jun 14, 2011 at 10:59 DanielB 20.2k2 gold badges46 silver badges52 bronze badges asked Jun 14, 2011 at 10:53 JohnJohn 331 silver badge3 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

You have to write:

window.onload = initAll;

At the point of execution the document element does not exists, because you don't call the function on load. You call the initAll() and assigning its return value to window.onload which is undefined or raising the error.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745294818a4621075.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信