Execute inline javascript in wordpress after page fully loaded

I am trying to find out the time it takes to fully load a page in wordpress using javascript.My JavaScript code is inline.I am using wp_print_scripts hook but as it prints scripts in document head i have doubt that i am not getting the time after full page load. i am not getting the correct time as body and footer content is yet to be executed/loaded.So i think i am getting the time it takes to load the head not the whole page.I can not use wp_footer and shutdown hooks because my page is cached.So how can i run my code to get the time after full page load without clearing/flushing the cache? Here is my code:

my-plugin.php

function print_script() {

    script type="text/javascript"
    window.onload = function () {
        var loadTime = window.performance.timing.domContentLoadedEventEnd-window.performance.timing.navigationStart;
        alert(loadTime);
        console.log(loadTime);
    }
            var var1 = ?php echo json_encode('var1'); ?;
            var var2 = ?php echo json_encode('var2'); ?;
            var var3 = ?php echo json_encode('var3'); ?;

    /script


  }
add_action('wp_print_scripts', 'print_script');

Topic cache plugins Wordpress javascript

Category Web


I think you should in this case use echo:

function print_script() {

echo '<script type="text/javascript">
window.onload = function () {
    var loadTime = window.performance.timing.domContentLoadedEventEnd-window.performance.timing.navigationStart;
    alert(loadTime);
    console.log(loadTime);
}
        var var1 = <?php echo json_encode('var1'); ?>;
        var var2 = <?php echo json_encode('var2'); ?>;
        var var3 = <?php echo json_encode('var3'); ?>;

</script>';


}
 add_action('wp_print_scripts', 'print_script');

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.