www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Client-Side Development > JavaScript

    JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...)

    Reply
     
    Thread Tools Rate Thread Display Modes
      #1  
    Old 01-27-2006, 12:42 AM
    X-ray's Avatar
    X-ray X-ray is offline
    M3T4L FR34K
     
    Join Date: Apr 2005
    Location: Belgium
    Posts: 89
    Question Chat App with XMLhttp request not working

    Hi all,

    I've upped this project of mine to make it possible for you guys to see what's wrong. the idea is to have a page refreshed every 3 seconds but when I use
    element.innerHTML = xmlhttp.responseXML;
    it say's Loading... and then returns an empty page
    when I use:
    element.innerHTML = xmlhttp.responseTXT;
    it displays "undefined"
    when I watch the source of the frame though the html tags are there really!
    how can I fix this?

    this is the script:
    (I'm trying to keep it as simple as possible)
    script.js
    Code:
    var xmlhttp=false; 
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') { 
      xmlhttp = new XMLHttpRequest(); 
    } 
    
    function loadFragmentInToElement(fragment_url, element_id) { 
        var element = document.getElementById(element_id); 
        element.innerHTML = 'Loading ...'; 
        xmlhttp.open("GET", fragment_url); 
        xmlhttp.onreadystatechange = function() { 
          if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
          element.innerHTML = xmlhttp.responseXML; 
          } 
        } 
        xmlhttp.send(null);
        window.setTimeout("loadFragmentIntoElement('chat.php','chatBody')", 1000);
    }
    this is the source of chat.php
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
    <html>
    <head>
    <script type="text/javascript">
    function goToBottom(){
    window.scroll(0,1000);
    }
    </script>
    <script type="text/javascript" src="script.js"></script>
    </head>
    <body style="margin:3px;" onLoad="goToBottom();loadFragmentInToElement('chat.php', 'Chat');">
    <div id="Chat" name="Chat">
    <?PHP
    db_connect("jvdev_be_-_chess","SELECT * FROM chat ORDER BY Id ASC");
    if($amount < 1){
    	echo "
    	Nothing has been said yet! <br />Grab the opportunity :D
    	";
    }
    else{
    	echo "
    	<table style=\"width:100%;border-width:0px;\" cellspacing=\"0\">
    	";
    	$clr = "#999999";
    	while($dbRow = mysql_fetch_assoc($dbResult)){
    		if(!isset($dbRow['Color'])){
    			$color="black";
    		}
    		else{
    			$color = $dbRow['Color'];
    		}
    		echo "
    		<tr><td style=\"width:100px;background-color:$clr;\">{$dbRow['Name']}</td><td style=\"background-color:$clr;\"><span style=\"color:#$color\">{$dbRow['Content']}</span></td></tr>
    		";
    		$last = $dbRow['Date'];
    		if($clr == "#999999"){
    			$clr = "#FFFFFF";
    		}
    		else{
    			$clr = "#999999";
    		}
    	}
    	echo "
    	</table>
    	<div name=\"bottom\" style=\"color:#666666;text-align:right;\">Last message: $last</span>
    	";
    }
    ?>
    </div>
    </body>
    </html>
    Could this have anything to do with the DOCTYPE?
    __________________
    http://www.jvdev.be

    \m/ Metal 4 Life \m/

    Last edited by X-ray; 01-27-2006 at 12:44 AM.
    Reply With Quote
      #2  
    Old 01-27-2006, 02:12 AM
    X-ray's Avatar
    X-ray X-ray is offline
    M3T4L FR34K
     
    Join Date: Apr 2005
    Location: Belgium
    Posts: 89
    Hmm, after some modifications i've got another error...

    in FF: element has no properties...

    the script:

    Code:
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') { 
      xmlhttp = new XMLHttpRequest(); 
    } 
    
    function loadFragmentInToElement(fragment_url, element_id) { 
        var element = document.getElementById(element_id); 
        element.innerHTML = 'Loading ...'; 
        xmlhttp.open("GET", fragment_url); 
        xmlhttp.onreadystatechange = function() { 
          if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
          element.innerHTML = xmlhttp.responseXML; 
          } 
        } 
        xmlhttp.send(null);
        window.setTimeout("loadFragmentInToElement('chat.php','Chat')", 1000);
    } 
    
    window.onLoad = loadFragmentInToElement('chat.php','Chat');
    Chat is an existing div element:

    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title></title>
    <script type="text/javascript" src="script.js"></script>
    </head>
    <body style="margin:3px;">
    <div id="Chat">
    Some PHP code here
    </div>
    </body>
    </html>
    __________________
    http://www.jvdev.be

    \m/ Metal 4 Life \m/
    Reply With Quote
      #3  
    Old 01-27-2006, 02:31 AM
    Ultimater's Avatar
    Ultimater Ultimater is offline
    Eccentric Tatertot
     
    Join Date: Jan 2005
    Location: Los Angeles, CA
    Posts: 4,828
    window.onLoad = loadFragmentInToElement('chat.php','Chat'); will be executed without waiting for the onload event handler. Try this:
    Code:
    window.onload = function(){loadFragmentInToElement('chat.php','Chat')};
    Reply With Quote
      #4  
    Old 01-27-2006, 02:36 AM
    X-ray's Avatar
    X-ray X-ray is offline
    M3T4L FR34K
     
    Join Date: Apr 2005
    Location: Belgium
    Posts: 89
    myeah that wasn't really the error....
    this line gives errors:

    var element = document.getElementById('chat');

    it has no properties according to FF
    and "null" is null or not an object in IE

    can anybody think of a reason?

    thanks

    EDIT: just to state the obvious:
    the <div id="chat"> DOES exist
    I left out the name="chat" because that gives errors in the W3C validator
    __________________
    http://www.jvdev.be

    \m/ Metal 4 Life \m/

    Last edited by X-ray; 01-27-2006 at 02:39 AM.
    Reply With Quote
      #5  
    Old 01-27-2006, 02:55 AM
    Ultimater's Avatar
    Ultimater Ultimater is offline
    Eccentric Tatertot
     
    Join Date: Jan 2005
    Location: Los Angeles, CA
    Posts: 4,828
    Try passing a third argument:
    Code:
    xmlhttp.open("GET", fragment_url, true);

    Last edited by Ultimater; 01-27-2006 at 02:58 AM.
    Reply With Quote
      #6  
    Old 01-27-2006, 03:03 AM
    X-ray's Avatar
    X-ray X-ray is offline
    M3T4L FR34K
     
    Join Date: Apr 2005
    Location: Belgium
    Posts: 89
    Quote:
    Originally Posted by Ultimater
    Try passing a third argument:
    Code:
    xmlhttp.open("GET", fragment_url, true);
    ah great... now I got rid of that error,
    and there's another one....

    xmlhttp.responseXML doesnt seem to return anything
    (and when I try responseTXT it says undefined...)

    where are the Ajax guru's out there?
    __________________
    http://www.jvdev.be

    \m/ Metal 4 Life \m/
    Reply With Quote
      #7  
    Old 01-27-2006, 03:06 AM
    Ultimater's Avatar
    Ultimater Ultimater is offline
    Eccentric Tatertot
     
    Join Date: Jan 2005
    Location: Los Angeles, CA
    Posts: 4,828
    Have you tried xmlhttp.responseText?
    Reply With Quote
      #8  
    Old 01-27-2006, 03:18 AM
    X-ray's Avatar
    X-ray X-ray is offline
    M3T4L FR34K
     
    Join Date: Apr 2005
    Location: Belgium
    Posts: 89
    omg, you must be... an angel!

    ok now it works in firefox but IE still gives error...

    object doesn't support this property or method (line 25...)

    line 25... that doesn't make any sense, there's not even JS in there :X

    and also I want it to scroll down the page all the time... how would you do that?
    window.scroll(0,1000); doesnt work when I put it there:

    Code:
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') { 
      xmlhttp = new XMLHttpRequest();
    } 
    
    function loadFragmentInToElement(fragment_url) { 
        var element = document.getElementById('chat'); 
        element.innerHTML = 'Loading ...'; 
        xmlhttp.open("GET", fragment_url, true); 
        xmlhttp.onreadystatechange = function() { 
          if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
          element.innerHTML = xmlhttp.responseText; 
          } 
        } 
        xmlhttp.send(null);
        window.scroll(0,1000);
        window.setTimeout("loadFragmentInToElement('chat.php')", 3000);
    } 
    
    
    window.onload = function(){loadFragmentInToElement('chat.php')};
    __________________
    http://www.jvdev.be

    \m/ Metal 4 Life \m/
    Reply With Quote
      #9  
    Old 01-27-2006, 03:22 AM
    Ultimater's Avatar
    Ultimater Ultimater is offline
    Eccentric Tatertot
     
    Join Date: Jan 2005
    Location: Los Angeles, CA
    Posts: 4,828
    Get rid of:
    Code:
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') { 
      xmlhttp = new XMLHttpRequest(); 
    }
    and use:
    Code:
    var xmlhttp=false;
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
    // JScript gives us Conditional compilation, we can cope with old IE versions.
    // and security blocked creation of the objects.
     try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
      try {
       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
       xmlhttp = false;
      }
     }
    @end @*/
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    	try {
    		xmlhttp = new XMLHttpRequest();
    	} catch (e) {
    		xmlhttp=false;
    	}
    }
    if (!xmlhttp && window.createRequest) {
    	try {
    		xmlhttp = window.createRequest();
    	} catch (e) {
    		xmlhttp=false;
    	}
    }
    in a JS external file, http://jibbering.com/2002/4/httprequest.html , IE doesn't understand the XMLHttpRequest method.
    Reply With Quote
      #10  
    Old 01-27-2006, 03:26 AM
    X-ray's Avatar
    X-ray X-ray is offline
    M3T4L FR34K
     
    Join Date: Apr 2005
    Location: Belgium
    Posts: 89
    but why? is that a way for IE to annoy ppl?
    I mean... I don't feel like making my page W3C invalid for bloody IE

    is there a way around this?

    btw: thank you a lot...
    I had that code commented... seems to work better, now I can see the
    entries in IE, but I still can reload the ?
    __________________
    http://www.jvdev.be

    \m/ Metal 4 Life \m/

    Last edited by X-ray; 01-27-2006 at 03:30 AM.
    Reply With Quote
      #11  
    Old 01-27-2006, 03:30 AM
    Ultimater's Avatar
    Ultimater Ultimater is offline
    Eccentric Tatertot
     
    Join Date: Jan 2005
    Location: Los Angeles, CA
    Posts: 4,828
    It is a cross-browser way, including IE, to set the xmlhttp user-defined varaible to the XMLHttpRequest object.
    Reply With Quote
      #12  
    Old 01-27-2006, 03:33 AM
    X-ray's Avatar
    X-ray X-ray is offline
    M3T4L FR34K
     
    Join Date: Apr 2005
    Location: Belgium
    Posts: 89
    that's not what I meant...
    I meant: "why does IE not recognise XMLhttp in external .js files?"
    and that's what's bothering me
    is there a way around that?
    __________________
    http://www.jvdev.be

    \m/ Metal 4 Life \m/
    Reply With Quote
      #13  
    Old 01-27-2006, 03:39 AM
    Ultimater's Avatar
    Ultimater Ultimater is offline
    Eccentric Tatertot
     
    Join Date: Jan 2005
    Location: Los Angeles, CA
    Posts: 4,828
    IE understands it, have you tested it online with IE?

    Also, your scrolling functionality:
    Code:
    window.scroll(0,1000);
    Will scroll to the exact coords the first time, and the exact same coords the second time.

    If you want it to scroll gradually, a bit at a time scrolling lower and lower, then you'd want to use the scrollBy method instead:
    Code:
    window.scrollBy(0,1000);
    However, 1000 pixels at a time is allot of page, try starting with 100:
    Code:
    window.scrollBy(0,100);
    Reply With Quote
      #14  
    Old 01-27-2006, 03:53 AM
    X-ray's Avatar
    X-ray X-ray is offline
    M3T4L FR34K
     
    Join Date: Apr 2005
    Location: Belgium
    Posts: 89
    aargh...

    how did I mess this up?

    http://www.jvdev.be/chess/
    could you take a look? (click Chat to open popup)

    thank you
    __________________
    http://www.jvdev.be

    \m/ Metal 4 Life \m/
    Reply With Quote
      #15  
    Old 01-27-2006, 04:05 AM
    Ultimater's Avatar
    Ultimater Ultimater is offline
    Eccentric Tatertot
     
    Join Date: Jan 2005
    Location: Los Angeles, CA
    Posts: 4,828
    Edit: http://www.jvdev.be/chess/chat/chat.php and change:
    Code:
    <script type="text/javascript" src="script.js"></script>
    into:
    Code:
    <script type="text/javascript" src="httprequest.js"></script>
    <script type="text/javascript" src="script.js"></script>
    (adding the httprequest.js file prior to it)

    And change your scripts to look as follows:
    script.js
    Code:
    function loadFragmentInToElement(fragment_url) {
    if(!xmlhttp){alert("Non compatible browser for XMLHttpRequest object!");return false;}
        var element = document.getElementById('chat'); 
        xmlhttp.open("GET", fragment_url, true); 
        xmlhttp.onreadystatechange = function() { 
          if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
          element.innerHTML = xmlhttp.responseText; 
          window.scroll(0,1000);
          } 
        } 
        xmlhttp.send(null);
        window.setTimeout("loadFragmentInToElement('chat.php')", 1000);
    } 
    
    window.onload = function(){loadFragmentInToElement('chat.php')};
    httprequest.js
    Code:
    var xmlhttp=false;
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
    // JScript gives us Conditional compilation, we can cope with old IE versions.
    // and security blocked creation of the objects.
     try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
      try {
       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
       xmlhttp = false;
      }
     }
    @end @*/
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    	try {
    		xmlhttp = new XMLHttpRequest();
    	} catch (e) {
    		xmlhttp=false;
    	}
    }
    if (!xmlhttp && window.createRequest) {
    	try {
    		xmlhttp = window.createRequest();
    	} catch (e) {
    		xmlhttp=false;
    	}
    }
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools
    Display Modes Rate This Thread
    Rate This Thread:

    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 Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 01:07 PM.



    Acceptable Use Policy

    Internet.com
    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.