简体   繁体   中英

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM