1
我有一個PHP文件,它返回一個div和一個javascript。 我想換一個特定的div(實際上它的內容),所以適合於Ajax的代碼如下Ajax響應動態加載特定div中的返回javascript
<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。這裏是PHP代碼中newgraph.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;
會做的伎倆,但事實並非如此。任何幫助非常感謝。
不,它不起作用。我所看到的只是一個空的白頁,取代了前一個白頁。再加上jQuery只是一個剩餘的。我不使用它。 – NickolasZev 2012-04-07 09:57:00
出於好奇,你是對的。我有最低版本的jQuery沒有這樣做。現在我使用谷歌的api jquery,一切正常,就像你說的那樣....我覺得很愚蠢。我想它會脫機工作,如果我下載它,對吧? – NickolasZev 2012-04-07 10:10:00
是的,如果你下載它,它也會脫機工作。但這與「min」版本無關,應該與其他版本完全一樣。最小版本只是「最小」,所以所有不需要的空格,註釋等都被刪除以獲得儘可能低的文件大小。 – Marc 2012-04-07 10:17:20