0

For my Science Fair project I need to be able to find out how long a website takes to load every 10 seconds. The issue is I'm not sure how to load the website and check how long it takes to load. So far I've made a program that just tells you what you typed in a text box, all I need now is just how to check the website's load time. -Thank you in advance Here is the code

<html>
<body id='body'>
    <p>Website Checker</p>
    <textarea id='websiteurl' rows="1" cols="50"></textarea><br>
    <button onclick="checkhowlongittakes()">Input!</button>
</body>
</html>

<style>
    #body {
        background-color: coral;
    }    
</style>

<script>
    document.getElementById('websiteurl').value = 'Input the url here'
    function checkhowlongittakes() {
        weburl = document.getElementById('websiteurl').value
    //Checkhowlongthewebsitetakestoload(weburl)
    }
</script>
Harry
  • 33
  • 1
  • 1
  • 7

3 Answers3

1
window.onload = function () {
    var loadTime = window.performance.timing.domContentLoadedEventEnd-window.performance.timing.navigationStart; 
    console.log('Page load time is '+ loadTime);
}
Ajay
  • 196
  • 2
  • 12
1

If you are using Google Chrome then click F12 then a window pops-up. On that click network and refresh your page. you will get loading time of your each component on page including javascript.

Prateek
  • 1,229
  • 17
  • 31
0

To get a ping time for other websites:

function checkhowlongittakes() {
    weburl = document.getElementById('websiteurl').value;
    getLoadTime(weburl, function(time) {
        console.log("load time for " + weburl + " was " + time + " ms");
    });
}
function getLoadTime(url, callback) {
    var req = new XMLHttpRequest();
    var start = Date.now();
    req.onreadystatechange = function(e) {
        if (this.readyState == 4)
            callback(Date.now() - start);
    }
    req.open("GET", url);
    req.send();
}
<p>Website Checker</p>
<textarea id='websiteurl' rows="1" cols="50" placeholder="http(s)://">https://stacksnippets.net</textarea><br>
<button onclick="checkhowlongittakes()">Input!</button>
tklg
  • 2,572
  • 2
  • 19
  • 24