SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question JS problem in an Ajax script : "undefined" result in innerHTML

    hello,

    well i've a display problem in my ajax script, my script ask to the server a message every x seconds so when i receive it, i display it in a div element with innerHTML. But the first message of the first time i launch the script in my page i see 'undefined', like :

    undefined
    message 1
    message 2
    ...

    The problem is solved when i redo the script without reloading my page. Like an element which didn't exist yet... How can i fix this?

    thx
    Last edited by perezoso; Jul 12, 2006 at 08:09.

  2. #2
    SitePoint Addict dek's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Show us the script, and maybe we can help.
    Only dead fish go with the flow

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, sorry i thought it could be a generic problem, here it is :

    Code:
    <script>
    var messageCell;
    var listeMessage ;
    
    function doRequest() {
        var url = "...";
        initRequest(url);
        req.onreadystatechange = processRequest;
        req.send(null);
    }
    
    function processRequest() {
        if (req.readyState == 4) {
            if (req.status == 200) {
                var response = req.responseXML.documentElement;
                var message = response.getElementsByTagName('message')[0].firstChild.data;
                listeMessage += "<BR />" + message;
                showMessage(listeMessage);
            }
            ...
        }
    }
    
    function initMessageZone() {
        messageCell = window.document.getElementById("progress");
        messageCell.innerHTML = "Test<BR />"; // A try to initialise it but it's not displayed
    }
    
    function showMessage(message) {
        messageCell.innerHTML = message;
    }
    </script>
    
    <form onSubmit="initMessageZone();doRequest();">
    ...
    </form>
    
    <div id="progress"></div>
    I tried to test "messageCell" with "== undefined" but nothing...

  4. #4
    SitePoint Addict darkwater23's Avatar
    Join Date
    Nov 2005
    Location
    Omaha, NE
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Instead of "== undefined", maybe "if(message)" would work.

    Code:
    function showMessage(message){
        if(message){
            messageCell.innerHTML = message;
        }
    }

  5. #5
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, and if(messageCell) or if(messageCell.innerHtml) neither :/

  6. #6
    SitePoint Enthusiast ada80ro's Avatar
    Join Date
    Apr 2005
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have to stop the default action onsubmit which is to submit the form because when you submit a form all the activity in that page is canceled.

    <form onSubmit="initMessageZone();doRequest(); return false">

  7. #7
    SitePoint Addict dek's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wayhey! New one on me, this

    Easily fixed though. You never assign an initial value to listeMessage. So when you run this line:

    Code:
     listeMessage += "<BR />" + message;
    It's adding "<BR />" + message; to 'undefined'.

    Just assign it at the start:
    Code:
    var messageCell;
    var listeMessage = '';
    Only dead fish go with the flow

  8. #8
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's working !!! Wonderful !

    The assignment solve the problem and i added the "return false" just in case.

    Thx everyone, you're the bests


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •