SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict Sojan80's Avatar
    Join Date
    May 2002
    Location
    Central WI, US
    Posts
    262
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    AJAX, Safari 2.0, and IE 6

    I am writing a little Ajax piece to post small little news snippets to and HTML page. I however do not seem to be able to get it to fire off at all in Safari and in IE for it to show up I need to dump the temporary internet files to see it and once I refresh the page it goes away again. I need this to work in both IE 6 and in Safari so can someone spot what I am not doing that I should be doing and let me know how to fix it...
    here's the JavaScript
    Code:
     
    <script type="text/javascript" language="javascript">
    function makeHttpRequest(url, callback_function, return_xml)
    {
       var http_request = false;
    
       if (window.XMLHttpRequest) { // Mozilla, Safari,...
    	   http_request = new XMLHttpRequest();
    	   if (http_request.overrideMimeType) {
    		   http_request.overrideMimeType('text/xml [15]');
    	   }
       } else if (window.ActiveXObject) { // IE
    	   try {
    		   http_request = new ActiveXObject("Msxml2.XMLHTTP");
    	   } catch (e) {
    		   try {
    			   http_request = new ActiveXObject("Microsoft.XMLHTTP");
    		   } catch (e) {}
    	   }
       }
    
       if (!http_request) {
    	   alert('Unfortunatelly you browser doesn\'t support this feature.');
    	   return false;
       }
       http_request.onreadystatechange = function() {
    	   if (http_request.readyState == 4) {
    		   if (http_request.status == 200) {
    			   if (return_xml) {
    				   //alert('Loading responseXML');
    				   eval(callback_function + '(http_request.responseXML)');
    			   } else {
    				   //alert('Load responseText')
    				   eval(callback_function + '(http_request.responseText)');
    			   }
    		   } else {
    			   alert('There was a problem with the request.(Code: ' + http_request.status + ')');
    		   }
    	   }
       }
       http_request.open('GET', url, true);
       http_request.send(null);
    }
    
    function writeEvents(xmldoc) {
    
       var result_div = document.getElementById('extras');
       var result_collection = xmldoc.getElementsByTagName('clip');
    
       var new_command = xmldoc.getElementsByTagName('news').item(0).firstChild.nodeValue;
       //result_div.appendChild(document.createTextNode(new_command));
    
       var number_of_items = result_collection.length;
       //debug statement
       //alert('num of items = ' + number_of_items);
    
       for (var i = 0; i < number_of_items; i++) {
    	   if (result_collection.item(i).hasChildNodes()) {
    	   //alert('clip has ' + result_collection.item(i).childNodes.length + ' child nodes');
    		   var result_wrap = document.createElement('div');
    		   result_wrap.setAttribute("class","event");
    		   
    		   var h6 = document.createElement('h5');
    		   var h6_txt = document.createTextNode(result_collection.item(i).getElementsByTagName('title').item(0).firstChild.nodeValue);
    		   h6.appendChild(h6_txt);
    		   //debug statement
    		   //alert(h6_txt);
    		   
    		   var para = document.createElement('p');
    		   var para_txt = document.createTextNode(result_collection.item(i).getElementsByTagName('text').item(0).firstChild.nodeValue);		   
    		   para.appendChild(para_txt);
    		   //debug statement
    		   //alert(para_txt);
    		   
    		   result_wrap.appendChild(h6); // append the title to the containing div element
    		   result_wrap.appendChild(para); // append the paragraph to the containing div element
    		   result_div.appendChild(result_wrap); // append the containing div element back to the result div
    		   
    	   } //end if
    	   
       }
    
    
       //document.getElementById('news').value = '';
    } // end writeEvents(xmldoc)
    
       makeHttpRequest('/design_skins/dept_twocolfluid/includes/news.xml','writeEvents',true);
    </script
    Here's the sample XML File
    Code:
    <?xml version="1.0" ?>
    <news>
      <clip>
    	<title>Spetember 12, 2006</title>		
    	<text>A presentationon on blah!</text>
      </clip>
      <clip>
    	<title>Spetember 24, 2006</title>		
    	<text>Another presentationon on blah!</text>
      </clip>
    </news>
    Any help would be greatly appreciated!

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,785
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    When you do multiple calls to the same URL with IE6 the browser decides to save the time for retrieving from the server again by using the copy it cached on the first call. There are two ways around this. Either set the returned content so that it doesn't get cached at all or add a dummy querystring to the end of the URL with the time in it so that each call is different. See http://javascript.about.com/library/blajax14.htm for more info.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Addict Sojan80's Avatar
    Join Date
    May 2002
    Location
    Central WI, US
    Posts
    262
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, that sounds like it might solve the IE problem, but how do I stop IE from caching the page at all?

    Also, once I do this will it work in Safari? Or do I need to do something different to make it work in Safari?

  4. #4
    SitePoint Addict Sojan80's Avatar
    Join Date
    May 2002
    Location
    Central WI, US
    Posts
    262
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dude that so rocks! Thanks. That actually fixed my problem in both Safari and IE!


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
  •