Passing Variables through different functions for an XMLHttpRequest script.

Discussion in 'Javascript' started by kasper48, Oct 20, 2006.

  1. kasper48

    kasper48 Guest

    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;
    }
    }
    }
    kasper48, Oct 20, 2006
    #1
    1. Advertising

  2. kasper48

    web.dev Guest

    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.
    web.dev, Oct 20, 2006
    #2
    1. Advertising

  3. kasper48

    Moses Guest

    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.
    Moses, Oct 21, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Yodai

    passing variables through functions...

    Yodai, Jan 15, 2004, in forum: C Programming
    Replies:
    13
    Views:
    582
  2. willie
    Replies:
    3
    Views:
    118
    Martin Honnen
    Sep 24, 2005
  3. fool
    Replies:
    0
    Views:
    105
  4. Replies:
    0
    Views:
    90
  5. ithinc
    Replies:
    4
    Views:
    255
    Thomas 'PointedEars' Lahn
    Apr 18, 2008
Loading...

Share This Page