M
magix
I got this error "document.getElementById(..) is null or not an
object,
and it puzzled me, why dTimer (the field that I defined) is getting
"undefined"
I have try to locate the rootcause, but I still not able to fix it yet
I'm using AJAX.
Here are the codes, and I wish someone can help me to fix the error
I got two files:
main_page.asp
child_page.asp
from main_page.asp, it will sendRequest via AJAX to child_page.asp
extracted code in main_page.asp:
================================
<script language="Javascript1.2" src="../scripts/xmlhttp.js"></script>
<script language="javascript">
var milisec=0;
var seconds =30;
var flag=0;
function displayTimer()
{
if (milisec<=0){
milisec=9 ;
seconds-=1 ;
}
if (seconds<=-1){
milisec=0 ;
seconds+=1 ;
... // some code here
SendRequest(1);
... // some code here
}
else
milisec-=1 ;
document.getElementById('dTimer').innerHTML= seconds
+"."+milisec ;
//THIS IS WHERE THE PROBLEM (pop up trigger exception) IS THAT
dTimer is NULL/NOT AN OBJECT from AJAX Request.
// SendRequest(0) from body onload will not go through this ELSE
statement, but sendRequest(1) from button in child_page.asp will.
setTimeout("displayTimer()",100) ;
}
var http = getXMLHTTPRequest();
function sendRequest(q)
{
var myurl = 'child_page.asp?q=';
myRand = parseInt(Math.random()*999999999999999);
var modurl = myurl + q + "&rand=" + myRand;
http.open("GET", modurl, true);
http.onreadystatechange = handleResponse;
http.send(null);
}
function handleResponse()
{
if(http.readyState == 4 && http.status == 200){
var response = http.responseText;
if(response) {
document.getElementById("ContentListing").innerHTML =
response;
milisec=0 ;
seconds = 30 ;
// if I do alert("dTimer" +
document.getElementById('dTimer').value); here, I will get
dTimer=undefined
displayTimer(); // THIS IS WHERE THE TIMER BEGIN AFTER
THE QUESTION is LOADED FROM AJAX RESPONSE.
}
}
else if(http.readyState == 4 && http.status == 500){
var response = http.responseText;
if(response) {
document.getElementById("ContentListing").innerHTML =
'<br><span class="gray">Error code: ' + http.status + '<br>Info: ' +
http.statusText + '</span>' ;
}
}
else {
document.getElementById('ContentListing').innerHTML = '<img
src="../images/ani_loading.gif" align="absmiddle"><span style="font-
family:arial; font-size:8pt; font-weight:bold;"> Processing your
request. Please wait...</span>';
}
}
</SCRIPT>
<body onload="SendRequest(0)" bgcolor="#FFFFFF" marginwidth="4"
leftmargin="4" marginheight="0" topmargin="0">
<table border="0" cellpadding="0" cellspacing="0" style="border-
collapse: collapse" bordercolor="#111111" width="100%">
<div id="ContentListing"></div>
<input type="button" .... onClick="SendRequest(1)">
</table>
extracted code in child_page.asp:
================================
<table width="100%" border="0" cellspacing="0" cellpadding="0"
align="left">
<span id="dTimer"></span>
.... <!-- some code here->
</table>
================================
So the point is I already define the id = "dTimer" in my
child_page.asp, but
it showed that dTimer is undefined since start. I even use alert(..)
to trace the output of
document.getElementById('dTimer').value, and it said undefined. Is it
because the dTimer is dynamic, etc ?
Can you help to explain why the dTimer is undefined ? and how to fix
it ?
object,
and it puzzled me, why dTimer (the field that I defined) is getting
"undefined"
I have try to locate the rootcause, but I still not able to fix it yet
I'm using AJAX.
Here are the codes, and I wish someone can help me to fix the error
I got two files:
main_page.asp
child_page.asp
from main_page.asp, it will sendRequest via AJAX to child_page.asp
extracted code in main_page.asp:
================================
<script language="Javascript1.2" src="../scripts/xmlhttp.js"></script>
<script language="javascript">
var milisec=0;
var seconds =30;
var flag=0;
function displayTimer()
{
if (milisec<=0){
milisec=9 ;
seconds-=1 ;
}
if (seconds<=-1){
milisec=0 ;
seconds+=1 ;
... // some code here
SendRequest(1);
... // some code here
}
else
milisec-=1 ;
document.getElementById('dTimer').innerHTML= seconds
+"."+milisec ;
//THIS IS WHERE THE PROBLEM (pop up trigger exception) IS THAT
dTimer is NULL/NOT AN OBJECT from AJAX Request.
// SendRequest(0) from body onload will not go through this ELSE
statement, but sendRequest(1) from button in child_page.asp will.
setTimeout("displayTimer()",100) ;
}
var http = getXMLHTTPRequest();
function sendRequest(q)
{
var myurl = 'child_page.asp?q=';
myRand = parseInt(Math.random()*999999999999999);
var modurl = myurl + q + "&rand=" + myRand;
http.open("GET", modurl, true);
http.onreadystatechange = handleResponse;
http.send(null);
}
function handleResponse()
{
if(http.readyState == 4 && http.status == 200){
var response = http.responseText;
if(response) {
document.getElementById("ContentListing").innerHTML =
response;
milisec=0 ;
seconds = 30 ;
// if I do alert("dTimer" +
document.getElementById('dTimer').value); here, I will get
dTimer=undefined
displayTimer(); // THIS IS WHERE THE TIMER BEGIN AFTER
THE QUESTION is LOADED FROM AJAX RESPONSE.
}
}
else if(http.readyState == 4 && http.status == 500){
var response = http.responseText;
if(response) {
document.getElementById("ContentListing").innerHTML =
'<br><span class="gray">Error code: ' + http.status + '<br>Info: ' +
http.statusText + '</span>' ;
}
}
else {
document.getElementById('ContentListing').innerHTML = '<img
src="../images/ani_loading.gif" align="absmiddle"><span style="font-
family:arial; font-size:8pt; font-weight:bold;"> Processing your
request. Please wait...</span>';
}
}
</SCRIPT>
<body onload="SendRequest(0)" bgcolor="#FFFFFF" marginwidth="4"
leftmargin="4" marginheight="0" topmargin="0">
<table border="0" cellpadding="0" cellspacing="0" style="border-
collapse: collapse" bordercolor="#111111" width="100%">
<div id="ContentListing"></div>
<input type="button" .... onClick="SendRequest(1)">
</table>
extracted code in child_page.asp:
================================
<table width="100%" border="0" cellspacing="0" cellpadding="0"
align="left">
<span id="dTimer"></span>
.... <!-- some code here->
</table>
================================
So the point is I already define the id = "dTimer" in my
child_page.asp, but
it showed that dTimer is undefined since start. I even use alert(..)
to trace the output of
document.getElementById('dTimer').value, and it said undefined. Is it
because the dTimer is dynamic, etc ?
Can you help to explain why the dTimer is undefined ? and how to fix
it ?