我加入這個JS代碼:onload不能調用我的JS函數?
<script language="JavaScript" type="text/JavaScript">
var receiveReq = getXmlHttpRequestObject();
var mTimer;
var url = "www-rainbowcode-net/apps_dev.php/messagebox/list";
function getXmlHttpRequestObject()
{
alert("in gethttprequest");
if (window.XMLHttpRequest)
{
return new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
return new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
//document.getElementById('p_status').innerHTML = 'Status: Cound not create XmlHttpRequest Object.';
}
}
function getNewMessage()
{
if (receiveReq.readyState == 4 || receiveReq.readyState == 0)
{
alert("state is" + receiveReq.readyState);
//var params = "name" + name + "&" + "comment=" + comment;
receiveReq.open("POST", url, true);
receiveReq.onreadystatechange = processReqChange;
receiveReq.send(null);
}
}
function processReqChange()
{
// only if req shows "loaded"
if (receiveReq.status == 200)
{
alert("processed" + receiveReq.responseText);
document.getElementById("message_area").innerHTML = receiveReq.responseText;
}
else
{
alert("There was a problem retrieving the XML data:\n" +
receiveReq.statusText);
}
mTimer = setTimeout('getNewMessage();',2000);
}
</script>
然後我有我的HTML用PHP混合
<body onload = "return getNewMessage();">
<table width='96%' border='0'>
<?php
$cursor = $pager->getFirstIndice();
foreach ($pager->getResults() as $msg)
{
$has_freechat = false;
//changed id to withid here
$freechat_req_link="profiles/confirmfreechat?withid=".$msg->getRcProfileTableRelatedByProfileIdFrom()->getId();
$freechat_req_link=link_to('Freechat',$freechat_req_link,'class=link_small_dark');
$cc = sizeof ($fc_records);
for($i = 0; $i < $cc; $i++)
{
if($fc_records[$i]->getProfileIdWith() == $msg->getProfileIdFrom())
{
$has_freechat = true;
break;
}
}
$unique_code_from = $msg->getRcProfileTableRelatedByProfileIdFrom()->getUniqueCode();
$block_url = link_to('Block User',"blocklist/block?unqiue_code=$unique_code_from",'class=link_medium_blue');
echo "<div id = 'message_area'>";
echo "<tr>";
$date = add_date($msg->getCreatedAt(),$hr=2);
echo "<td class='td_show_contact_item' align='left'>".$date."</td>";
$opened_once = $msg->getOpenedOnce();
if($opened_once >= 1)
{
echo "<td class='td_show_contact_item' align='left'>".link_to($msg->getSubject(), 'messagebox/read?cursor='.$cursor,'class=link_medium_blue')."</td>";
}
else
{ ?>
<td align='left'>
<a href="<?php echo url_for('messagebox/read?cursor=').$cursor ?>" style='color:#ff0000 !important' class='spn_small_red_rbc'><?php echo $msg->getSubject();?> </a>
</td>
<?php
}
echo "<td class='td_show_contact_item' align='left'>".$unique_code_from." ($block_url)</td>";
echo "</tr>";
echo "</div>";
++$cursor;
}
</table>
有誰能夠告訴我爲什麼我在第二和第三功能警報不執行呢?一個在第一執行
感謝
不使用return關鍵字 –
刪除,並添加行MKK建議,但無明顯差異??? –