0

Adding window change listener in JavaScript

JavaScript
// listen for window reload or part of the page reload
var oldHref = document.location.href;

window.onload = function() {
    var bodyList = document.querySelector("body")
        ,observer = new MutationObserver(function(mutations) {
            mutations.forEach(function(mutation) {
                if (oldHref != document.location.href) {
                    oldHref = document.location.href;
                    /* Changed ! your code here */
                }
            });
        });

    var config = {
        childList: true,
        subtree: true
    };
    observer.observe(bodyList, config);
};
JavaScript
//listen for window resize event
window.addEventListener('resize', function(event){
    var newWidth = window.innerWidth;
    var newHeight = window.innerHeight; 
});
add comment
Suggest a different solution or add another example