admin管理员组

文章数量:1415073

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

本文标签: Javascript windowonload not working in IEStack Overflow