Phorce
Phorce

Reputation: 4642

Chrome Extension - EventListener when a page loads

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.

Upvotes: 0

Views: 4976

Answers (1)

grilly
grilly

Reputation: 450

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)

Upvotes: 2

Related Questions