9

I use content script in my Chrome extension. content scripts are injected at "document_start".

window.onload = function() {console.log("window onload event fired.");}

I run the above code in content script, but when I load a page, the onload event is not fired.
Is there anything wrong?

Leslie Wu
  • 760
  • 3
  • 13
  • 29

2 Answers2

3

Check with this Code

if (window.attachEvent) {window.attachEvent('onload', your_function);}
else if (window.addEventListener) {window.addEventListener('load', your_function, false);}
else {document.addEventListener('load', your_function, false);}
Baalu
  • 243
  • 2
  • 9
-2

It's working perfectly.In order to check whether the console.log is fired or not on windows onload event.

Run(Load) the Page/Script in Chrome.

Right click and select Inspect element.

Click on Console tab.

You will see "window onload event fired." on console box.

Screen Shot:

enter image description here

Ritesh Kumar Gupta
  • 5,055
  • 7
  • 45
  • 71
  • Also can you be a little more specific.Does windows onload event not fired at all? Or windows onload is fired but not working as expected(that is, before windows load, onload script is fired)? – Ritesh Kumar Gupta Mar 27 '13 at 05:33
  • Still failure. Maybe window onload is already fired before content script load. Now I use setInterval to check document.readyState. – Leslie Wu Mar 27 '13 at 06:58