2013-02-19 140 views
0

我想通過使用iframe找出頁面加載時間。我有一個網址數組,我把它放入一個iframe數組中,然後通過使用javascript獲得每個iframe的加載時間。我的問題 - 我的時間值不是一個數組,所以它只顯示1個值(它顯示的是第一幀的值,然後第二幀將其替換爲第二幀的值)。Javascript函數只返回1個值而不是數組數組

<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> 

在這段代碼中我做的URL($網址)的陣列,運行一個foreach循環使盡可能多的I幀的有網址,然後使用JavaScript我算的頁面加載時間。我得到加載時間後,我把它放入「加載時間」div。但就像我說的那樣,它只會放置1個值,而不是放置一組值。

它改變了這一點,但現在它不顯示任何內容:

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

追加到狀態的div而不是覆蓋它:

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

此外,聲明afterLoadsecondes作爲本地的功能:

var afterload = (new Date()).getTime(); 
var secondes = (afterload-beforeload)/1000; 
+0

我改變了代碼,但它沒有顯示ng現在任何值,我eddited我的帖子,因爲我不能在這裏寫代碼 – user1894929 2013-02-19 05:24:13

+0

你的腳本sholud之前/身體,而不是之後。也許是這樣嗎? – bfavaretto 2013-02-19 05:28:02