Re: Newbie Question - variable name in getElementById() method doesn't seem to work
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.javascript archive

Re: Newbie Question - variable name in getElementById() method doesn't seem to work

From: <spivee@gmail.com>
Date: Mon Jan 30 2006 - 23:25:39 CET

Thanks for the quick replies...

Majestik.. The page loads completely before handleResponse is called.
It doesn't get called until the onMouseOver event occurs. Handling the
response on load would defeat the purpose of the test, since I'm trying
to get new content to load when a user creates an event like
onMouseOver().

McKirahan,

I am changing the element twice just to test. The first one gives what
the value of id is at that point and the second will change assuming
that the document.getElementById(id).innerHTML is successfully updated.

I hadn't thought of commenting out sendRequest(), but that seems to fix
the problem, sorta.. since sendRequest() is important to the entire
point of the script..

Here's the entire code, slightly modified since last time, but
basically, the same thing. If this is run as is, the print I get in
handleResponse() shows the id is undefined. If I comment out
sendRequest(), then the id successfully passes through. I've no clue
how this is changing the id variable since that variable isn't passed
to sendRequest..

I built this code using several things I've found on the internet.. I
think my problem is with the line in sendRequest()
        http.onreadystatechange = handleResponse;
Not entirely certain what that's doing.... However, if you change line
37 and set id to 'myTest', then the script works fine..

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
<head>

<script language="JavaScript">
<!-- hide from non java browsers

function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

var http = createRequestObject();

function sendRequest(url) {
    http.open('get', url);
    http.onreadystatechange = handleResponse;
    http.send(null);
}

function handleResponse(id) {

    document.getElementById('handleresponse').innerHTML = "The ID in
handleResponse() is " + id;
    if(http.readyState == 4){
        var response = http.responseText;
        var update = new Array();
        if(response.indexOf('|' != -1)) {
            update = response.split('|');
            var data = update[0];
            document.getElementById(id).innerHTML = data;
        }
    }
}

function doGetUrl(id,url) {
   document.getElementById('geturl').innerHTML = "The ID in doGetURL()
is " + id;
   sendRequest(url);
   handleResponse(id);
}

-->
</script>
</head>

<body>
<div style="position:absolute;" id="myTest">myTest</div>
<br><br>
<a href="#" id="myHref"
onMouseOver="doGetUrl('myTest','myfile')">change content</a>
<div id="geturl"></div>
<div id="geturl2"></div>
<div id="handleresponse"></div>
</body>
</html>
Received on Tue Feb 7 21:28:00 2006