2013-03-21 34 views
1

得到这个JavaScript从txt文件中读取数据(网址),然后将每个网址放在一个框架中,计算花费多少时间在iframe中加载该网址,然后将结果显示为html div需要帮助修改JavaScript(插入一个PHP值)

下面是代码:

<script type="text/javascript"> 
    $.get("imones.txt", function (data) { 
     var array = data.split(/\r\n|\r|\n/); 
     var beforeLoad = (new Date()).getTime(); 
     var loadTimes = []; 
      var beforeTimes = [];    
     $('#frame_id').on('load', function() {         
      beforeTimes.push(beforeLoad); 
      loadTimes.push((new Date()).getTime()); 
      $('#frame_id').attr('src', array.shift()); 
       try { 
       $.each(loadTimes, function (index, value) { 
        var result = (value - beforeTimes[index])/1000; 
         if (result < 0) { 
          result = result * (-1); 
         } 
        $("#loadingtime" + [index]).html(result);      
        beforeLoad = value; 
       }); 
       } catch(ex) {} 
     }).attr('src', array.shift()); 
    }); 
</script> 

imones.txt写数据,它像这样 - example1.com, example2.com等等。而不是阅读本imones.txt文件,我想用PHP数组来替代它:

$url_array = array(); 
$url_array[] = 'example1.com'; 
$url_array[] = 'example2.com'; 

然后不是显示的结果为HTML DIV($("#loadingtime" + [index]).html(result);)我希望把这一结果到另一个PHP数组:

$php_array[] = $("#loadingtime" + [index]).html(result); 

有人可以帮我做这个吗?

+0

PHP的服务器上运行。 JavaScript在浏览器中运行。因此,如果您想将文件中的数据放入PHP数组中,您需要编写读取该文件的PHP代码。 – 2013-03-21 13:42:15

回答

0

工作过这样的想法:

// frontend 
$.get(
    "path_to_get_url_array", // server function 
    function(url_array){ // this receive your $url_array data 
     $.each(url_array, function(i, item) { 
      // run your code over item to calculate time 

      // save loadingtime 
      var loadingtime = ''; 
      $.post(
        "path_to_save_loadingtime", // server function 
        { 
         loadingtime = $("#loadingtime" + [index]).html(result); 
        } 
      }); 
    }, 'json'); 

// backend get_url_array 
function get_url_array() 
{ 
    // get $url_array from somewhere 
    echo json_encode($url_array); 
    exit; 
} 

// backend save_loadingtime 
function save_loadingtime() 
{ 
    $loadingtime = $_REQUEST['loadingtime']; 
    // save $loadingtime to db 
} 

// when you want acess you data get it 
function create_array() 
{ 
    // read all $loadingtime values from db  
    // populate $php_array 
    return $php_array; 
}