473,509 Members | 2,950 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing Variables through different functions for an XMLHttpRequest script.

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;
}
}
}

Oct 20 '06 #1
2 1766

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.

Oct 20 '06 #2

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.
Oct 21 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
6509
by: Kevin Lyons | last post by:
Hello, I am trying to get my select options (courses) passed correctly from the following URL: http://www.dslextreme.com/users/kevinlyons/selectBoxes.html I am having difficulty getting the...
3
14908
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
58
10049
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
8
1269
by: paladin.rithe | last post by:
I seem to recall seeing an article somewhere about how to easily make functions that just set the variables. I have no idea where that was, or what to look for. I think it defined one function,...
4
7948
by: Bart | last post by:
Hello, I created a php script in which I call php fumcion which looks like this function confirm($vl) { ?> <head>
3
1607
by: qshobbes | last post by:
I have an interesting (horribly frustrating) problem here. My goal is to write a Euchre game in JavaScript. I am using a PHP script to update game information and retrieve the current game status....
2
5873
by: Geoff Cox | last post by:
Hello, The code below is aimed at passing the date in the yyyyMMdd format from the javascript calendar in an html file to the php in a another file which then searches a MySQL database. For...
1
2201
satterfieldben
by: satterfieldben | last post by:
I have a newbie question about passing variables between functions. I am wanting to use a drop down box to select a value. Then base on which was selected, it would create a variable and I would call...
1
2552
by: kkshansid | last post by:
i want to pass both variables($q1 and value of select) from this php page to java script so that i can get both variables in second php file srt.php <script type="text/javascript"...
0
7234
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7344
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7412
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7069
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3203
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
441
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.