0

I am trying to find out page loading time by using iframes. I have an array of urls, that i put into an array of iframes, and then i get the loadtime of each iframe by using javascript. My problem - my time values is not an array, so it just shows 1 value (it shows value of 1st frame then second later it replaces it with value of 2nd frame on top of it).

   <html>
    <body>   
    <center>
    <?php 
    $url = array('example.com', 'example2.com');
    foreach($url as $i){ 
    ?>
    <iframe onload="pageloadingtime();" width="505" height="505" scrolling="no" security="restricted" src="<?php echo "http://www.".$i; ?> "></iframe>
    <?php
    } 
    ?>
    <div id="loadingtime"></div>
    </center>
    </body>
    <script type="text/javascript">    
    beforeload = (new Date()).getTime();
    function pageloadingtime()
    {
        afterload = (new Date()).getTime();
        secondes = (afterload-beforeload)/1000;
        document.getElementById("loadingtime").innerHTML = "<font color='red'>(Page took " + secondes + " seconds to load)</font>";
    }   
    </script>
    </html>

In this code i make an array of urls ($url), run a foreach cycle making as many iframes as there are urls, and then using javascript i count the page loading time. After i get the loading time i put it into the "loadingtime" div. But like i said it only puts 1 value instead of putting an array of values.

changed it to this, but now its not displaying anything:

beforeload = (new Date()).getTime();
function pageloadingtime()
{
    var afterload = (new Date()).getTime();
    secondes = (afterload-beforeload)/1000;
    document.getElementById("loadingtime1").innerHTML += "<font color='red'>(Page took " + secondes + " seconds to load)</font><br>";
}

1 Answer 1

1

Append to the status div instead of overwriting it:

document.getElementById("loadingtime").innerHTML += "<font color='red'>(Page took " + secondes + " seconds to load)</font><br>";

Also, declare afterLoad and secondes as local to the function:

var afterload = (new Date()).getTime();
var secondes = (afterload-beforeload)/1000;
Sign up to request clarification or add additional context in comments.

2 Comments

i changed the code but its not displaying any values at all now, i eddited my post since i cant write the code in here
Your script sholud be before /body, not after it. Maybe it's that?

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.