Javascript window.onload not working in IE - Stack Overflow

I wrote code that should clear form when user open page. It's working in FF but not in IE, any ide

I wrote code that should clear form when user open page. It's working in FF but not in IE, any idea why?

window.onload = clearForm()

  function clearForm() 
 {  

("load event detected!");  

 };

I wrote code that should clear form when user open page. It's working in FF but not in IE, any idea why?

window.onload = clearForm()

  function clearForm() 
 {  

("load event detected!");  

 };
Share Improve this question edited Jun 26, 2012 at 11:06 Marcel Korpel 21.8k6 gold badges62 silver badges80 bronze badges asked Jun 26, 2012 at 9:18 Nasan ErtNasan Ert 411 silver badge5 bronze badges 1
  • 2 This has to be a duplicate. It has to be. :-) – T.J. Crowder Commented Jun 26, 2012 at 9:22
Add a ment  | 

2 Answers 2

Reset to default 4

This line:

window.onload = clearForm()

calls clearForm and then assigns its return value to window.onload, exactly like x = foo(); calls foo and assigns the result to x. Remove the parens:

window.onload = clearForm

Separately, I would strongly remend not relying on the horror that is automatic semicolon insertion. Always supply all required semicolons:

window.onload = clearForm;

(Amusingly, you don't need the one at the end of your function clearForm() { ... }, because that's a function declaration, not a statement. It's harmless, though.)

Change window.onload = clearForm() to window.onload = clearForm; otherwise because of () you are assigning result of your function to window.onload

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

相关推荐

  • Javascript window.onload not working in IE - Stack Overflow

    I wrote code that should clear form when user open page. It's working in FF but not in IE, any ide

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信