Passing Variables through different functions for an XMLHttpRequest script.

K

kasper48

Hello,

I am using the following script to load content into a DIV tag on my
webpage when an onclick calls the 'loadXMLDiv1' function...I am trying
to add a section variable in the 'loadXMLDiv1' function ('divname')
that will specify which Div ID to load the content into.

This line:

getObject(divname).innerHTML = req.responseText;

is where the content gets loaded, but no matter what I try, I cant
pass the info in 'divname' from the 'loadXMLDiv1' function to the
'processChange' function... I want to get info that is in 'divname' to
tell the script where to load the content from the request.

The code is below, of course, I am a newbie with Javascript and have
tried my best to solve this one myself.

Any help would be greatly appreciated.

Cheers,

Greg

---- code below!

<!--
var req = null;

function loadXMLDiv1(url, divname) {
// Internet Explorer
try { req = new ActiveXObject("Msxml2.XMLHTTP"); }
catch(e) {
try { req = new ActiveXObject("Microsoft.XMLHTTP"); }
catch(oc) { req = null; }
}

// Mozailla/Safari
if (req == null && typeof XMLHttpRequest != "undefined") {
req = new XMLHttpRequest();
}
// Call the processChange() function when the page has loaded
if (req != null) {
var div;
req.onreadystatechange = processChange;
req.open("GET", url, true);
req.send(null);
}
}

function processChange(evt) {
// The page has loaded and the HTTP status code is 200 OK
if (req.readyState == 4) {
if (req.status == 200) {

// Write the contents of this URL to the div layer
getObject(divname).innerHTML = req.responseText;
}
}
}
 
W

web.dev

kasper48 said:

There is no need for html comment delimiters.
var req = null;

function loadXMLDiv1(url, divname) { [...]
}

function processChange(evt) {
// The page has loaded and the HTTP status code is 200 OK
if (req.readyState == 4) {
if (req.status == 200) {

// Write the contents of this URL to the div layer
getObject(divname).innerHTML = req.responseText;
}
}
}

Your callback method, processChange(), will not know what 'divname' is
since you cannot pass anything to it. Using 'divname' in this matter
will mean the variable will be undefined. You will never be able to
pass anything to the method since it is to be used as a callback. One
solution is to declare a global variable, in which you'll set when you
call your loadXMLDiv1, however take care to note that since it is an
asynchronous operation, if you have multiple divs that you want to
change, your timing will be off.
 
M

Moses

insteed of

getObject(divname).innerHTML = req.responseText;

try

document.getElementById("div_id").innerHTML = req.responseText;

where div_id is the id of the div where u need to fill the response
text

regards
moses



web.dev said:
kasper48 said:

There is no need for html comment delimiters.
var req = null;

function loadXMLDiv1(url, divname) { [...]
}

function processChange(evt) {
// The page has loaded and the HTTP status code is 200 OK
if (req.readyState == 4) {
if (req.status == 200) {

// Write the contents of this URL to the div layer
getObject(divname).innerHTML = req.responseText;
}
}
}

Your callback method, processChange(), will not know what 'divname' is
since you cannot pass anything to it. Using 'divname' in this matter
will mean the variable will be undefined. You will never be able to
pass anything to the method since it is to be used as a callback. One
solution is to declare a global variable, in which you'll set when you
call your loadXMLDiv1, however take care to note that since it is an
asynchronous operation, if you have multiple divs that you want to
change, your timing will be off.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top