1

I want to find the load time of a locally hosted website. Is this way efficient? How can I display the time in a dialogue box?

<head>
<script type="text/javascript">
             var loadTime = window.performance.timing.domContentLoadedEventEnd- window.performance.timing.navigationStart;
        </script>
        </head>
        
<body>
<script type="text/javascript">
          window.onload = function () {
    var loadTime = ((window.performance.timing.domComplete- window.performance.timing.navigationStart)/1000)+" sec.";
    console.log('Page load time is '+ loadTime);
}
        </script>
</body>

5
  • 1
    Is there a reason you don't want to use your browser developer tools for this? It's much easier to profile everything that way Commented Nov 17, 2018 at 15:17
  • Do you mean from the network tab pressing F12? I could not get what was happening there so I wanted the easy way to read the load time. Commented Nov 17, 2018 at 15:27
  • Yes, and it's far easier to see the total time plus the per resource time that way. Look for the OnDomContentLoaded number if your browser supports it for the effective total render Commented Nov 17, 2018 at 15:44
  • Does the developer tool give accurate load time? Commented Nov 17, 2018 at 16:06
  • Yes, they do. In all modern browsers Commented Nov 17, 2018 at 16:20

1 Answer 1

1

@Samrat Shrestha This snippet work for you

<doctype html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
</script>
    <head>
        <script type="text/javascript">
            $(document).ready(function() {
                 console.log("Time until DOMready: ",window.performance.timing.loadEventEnd-window.performance.timing.navigationStart);
             });
        </script>
        <!-- do all the stuff you need to do -->
    </head>
    <body>

    </body>
</html>

The ready event occurs after the HTML document has been loaded. window.onload fires when the entire page loads (images, styles, etc.)

window.onload vs $(document).ready()

https://developer.mozilla.org/en-US/docs/Web/API/Navigation_timing_API#Examples

https://developer.mozilla.org/en-US/docs/Web/API/PerformanceTiming/loadEventEnd

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.