Skip to Content

visibilityChangeActions()

Actions to run on page visibility change.

JavaScript February 19, 2018

Usage

This function runs automatically, so it is not called manually. Is this incorrect?

Demo


Change tabs to see the log:

Additional Notes

This function essentially does the following:

  • If a prerender was detected, send an event to Google Analytics and prevent videos from playing.
  • If the page was hidden, trigger nebula_page_hidden, add a page-visibility-hidden class to the body, and pause all videos.
  • Otherwise, if the page has become visible, trigger nebula_page_visible and remove the page-visibility-hidden from the body. Note: This does not automatically resume videos.

Source File

Located in /assets/js/nebula.js on line 433.

JavaScript
nebula.visibilityChangeActions = function(){
    if ( document.visibilityState === 'prerender' ){ //Page was prerendered
        ga('send', 'event', 'Page Visibility', 'Prerendered', 'Page loaded before tab/window was visible', {'nonInteraction': true});
        nebula.pauseAllVideos(false);
    }

    if ( document.visibilitystate === 'hidden' ){ //Page is hidden
        nebula.dom.document.trigger('nebula_page_hidden');
        nebula.dom.body.addClass('page-visibility-hidden');
        nebula.pauseAllVideos(false);
    } else { //Page is visible
        nebula.networkAvailable();
        nebula.dom.document.trigger('nebula_page_visible');
        nebula.dom.body.removeClass('page-visibility-hidden');
    }
};

Override

To override or disable this JavaScript function, simply redeclare it with the exact same function name.

JavaScript
nebula.visibilityChangeActions = function(){
    //Write your own code here, leave it blank, or return false.
}