0

I'm trying to create a Chrome extension, and I need the event handler to listen on each page that get's loaded. Currently, (for testing purposes) I have the following:

function onPageLoad(event)
{
    alert("Page Loaded");

}



document.addEventListener('DOMContentLoaded', function() {

    document.addEventListener("DOMContentLoaded", onPageLoad, true);
});

This, however, does not display the alert message and I cannot for the lift of me seem to work out where I am going wrong.

Phorce
  • 4,424
  • 13
  • 57
  • 107
  • possible duplicate of [On page load event in Chrome extensions](http://stackoverflow.com/questions/9862182/on-page-load-event-in-chrome-extensions) – CBroe Sep 25 '14 at 10:58

1 Answers1

2

either:

function onPageLoad(event)
{
    alert("Page Loaded");
}

document.addEventListener("DOMContentLoaded", onPageLoad, true);

or:

document.addEventListener("DOMContentLoaded", function() { 
     alert("Page Loaded");
}, true);

should work.

In your code the outer DOMContentLoaded gets fired on page load and sets the eventlistener to call the function on page load. (but after that no other page load event gets fired)

grilly
  • 450
  • 4
  • 11