Ajax响应动态加载特定div中返回的javascript

时间:2012-04-07 09:43:37

标签: javascript ajax

我有一个php文件,它返回一个div和一个javascript。 我想更改一个特定的div(实际上是它的内容),所以Ajax的代码是以下

<script type="text/javascript">
function changemainpage(id) {


  if (window.XMLHttpRequest)
   { //code for IE7+. Ffirefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
   }  
   else { //code for IE6,IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   }


   xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState==4)
    { 
        HandleResponse(xmlhttp.responseText);
    }
   }

   xmlhttp.open("GET","./newgraph.php?id=unit"+id,true);
   xmlhttp.send(null);

}

function HandleResponse (response) {

    document.getElementById('content').innerHTML=response;
    document.getElementById('content').src=document.getElementById('content').src;
}


function ChangeMainContent(id, graph){
    $('#content').load('newgraph/index.php?id=' + id,'&graph='+graph);
}

</script>

现在问题在于,当内容被更改时,它不会被解析为javascript。这是newgraph.php中的php代码

<?php 
echo "<!--dygraphs are not happy when placed inside a <center> tag. If you want to center a Dygraph, put it inside a table a table with align=center set. For more options go to dygraphs.com/options-->\n
<div id=\"graphid\" onLoad='refresh()' style=\"width:320px; height:290px;\"></div>\n";

?>
<?php 
//$table="unit1";
$table=$_GET['id'];
$ylabel="EnergyReckVARh"; //Available are Error BIT(1), UTCOffsetMin SMALLINT, LocalDatetime DATETIME, EnergyDeliveredkWh INT, EnergyReceivedkWh INT, EnergyDelkVARh INT, EnergyReckVARh INT, ApparentPowerTotalkVA SMALLINT, RealPowerTotalkW SMALLINT, ReactivePowerTotalkVAR SMALLINT
$outfile='outfile.csv';

echo $table;
//delete file in case it exists
//unlink($outfile);

//show database table
    include './database/config.php';
    include './database/opendb.php';

    $csv_output.='LocalDatetime,';
    $csv_output.=$ylabel;
    $csv_output.="\n";

    $result=mysql_query("SELECT LocalDatetime,".$ylabel." FROM ".$table." ORDER BY LocalDatetime");
    $dbcols = mysql_num_fields($result);
    $dbrows = mysql_num_rows($result);
    while ($row=mysql_fetch_row($result)){
        for ($i=0;$i<$dbcols;$i++){
            $csv_output.=$row[$i].",";
        }
        $csv_output=substr_replace($csv_output,"",-1);  //remove that last ","
        $csv_output.="\n";
    };

    $handle=fopen($outfile, 'w') or die ("Can;t open file");
    fwrite($handle, $csv_output);
    fclose($handle);

    //close database
    include './database/closedb.php';

echo "<script type=\"text/javascript\">
    var datamatrix=[];\n";


    //\"2008-05-07 18:00:00,75\\n\" +
    //\"2008-05-07 18:05:00,90\\n\" +
    //\"2008-05-07 18:06:00,80\\n\",
echo    "var ylabel='".$ylabel."';
    var tablet='".$table."';";


 echo "  
   var opts =     {
    title: ylabel+' for '+tablet,   
    ylabel: ylabel,     
    //showRangeSelector: true,
    legend: 'always',
    //rollPeriod: 50,
    showRoller: true,
    rangeSelectorPlotFillColor: '#A7B1C4',
    rangeSelectorPlotStrokeColor: 'red',
    fillGraph: true,
    //rangeSelectorHeight: '30'

    };

    alert(\"New Javascript is running\");
    g = new Dygraph(

    // containing div
    document.getElementById(\"graphid\"),
    // CSV or path to a CSV file.
    //\"Date,$ylabel\\n\" +\".\"\n
    \"$outfile\",
    //\"outfile.csv\",
    opts
    );
";

echo "</script>";

echo "dbrows are $dbrows";

?>

php文件返回代码,但不会将其解析为javascript,即警报不会弹出新窗口!如果我从一开始就运行它,一切都很好。我虽然

    document.getElementById('content').src=document.getElementById('content').src;

会做到这一点,但事实并非如此。任何帮助非常感谢。

1 个答案:

答案 0 :(得分:1)

我不知道你为什么要创建自己的Xml请求,因为你已经在使用jQuery。

抛弃完整的JS代码并执行此操作:

function changemainpage(id) {
    $('#content').load('./newgraph.php?id=unit'+id);
}

jQuery也将执行传入页面中包含的JS代码。