Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Passing Variables through different functions for an XMLHttpRequest script.

Reply
Thread Tools

Passing Variables through different functions for an XMLHttpRequest script.

 
 
kasper48
Guest
Posts: n/a
 
      10-20-2006
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;
}
}
}

 
Reply With Quote
 
 
 
 
web.dev
Guest
Posts: n/a
 
      10-20-2006

kasper48 wrote:
> <!--


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.

 
Reply With Quote
 
 
 
 
Moses
Guest
Posts: n/a
 
      10-21-2006

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 wrote:

> kasper48 wrote:
> > <!--

>
> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reading php-mysql data through xmlhttprequest foraci@gmail.com Javascript 0 01-11-2007 09:01 AM
reading php/mysql data through xmlhttprequest fool Javascript 0 01-08-2007 04:07 PM
Having problem to get GIF file through XMLHttpRequest willie Javascript 3 09-24-2005 05:27 PM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
passing variables through functions... Yodai C Programming 13 01-15-2004 05:16 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57