Execute inline javascript in wordpress after page fully loaded

Question

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');
0
, , query 3 years 2020-07-15T11:10:25-05:00 0 Answers 67 views 0

Leave an answer

Browse
Browse