an issue with XMLhttpRequest

Discussion in 'JavaScript' started by asafche, Aug 4, 2009.

  1. #1
    hello.

    I'm having a headache with the XHR (XMLhttpRequest) in IE7.

    when i'm trying to reload with a pressed shift button (for cache cleaning) the browser alerts me that it cannot load the website, and it stops.
    when i hide the XHR line in the script, it doesn't repeat the problem, and because of the last i've mentioned, i presumed that the problem is about the XHR.

    the script is:
    
    var http = createRequestObject();
    function createRequestObject() {
    	var objAjax;
    	var browser = navigator.appName;
    	if(browser == "Microsoft Internet Explorer"){
    		objAjax = new ActiveXObject("Microsoft.XMLHTTP");
    	}else{
    		objAjax = new XMLHttpRequest();
    	}
    	return objAjax;
    }
    function getNewContent(){
    url="gallery/solari.txt";
    http.open('get',url);
    http.onreadystatechange = updateNewContent;
    http.send(null);
    return false;
    }
    
    function updateNewContent(){
    	if(http.readyState == 4){
    		x = http.responseText;
    		var gallery=x.split("#");
    		counter=(gallery.length-1);
    		for (i=1; i<=counter; i+=1) {
    			varim=gallery[i].split("|");
    			document.getElementById('main_content').innerHTML += "<a href='gallery/"+varim[0]+"' rel='lightbox[solari]' title='"+varim[2]+"'><img src='gallery/"+varim[1]+"' class='img_gal' alt='" + varim[2] + "'></a>";
    		}
    	}
    }
    
    getNewContent();
    Code (markup):

     
    asafche, Aug 4, 2009 IP
  2. lp1051

    lp1051 Well-Known Member

    Messages:
    163
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #2
    Hi, just a thought, but does it work better with different createRequestObject() function? Actually IE7 is using XMLHttpRequest object not ActiveXObject, so the condition about browser name is misleading...
    Try just some textbook function from W3C - http://www.w3schools.com/Ajax/ajax_browsers.asp
    Any better?
     
    lp1051, Aug 4, 2009 IP
  3. Unni krishnan

    Unni krishnan Peon

    Messages:
    237
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    0
    #3
    Unni krishnan, Aug 4, 2009 IP
  4. asafche

    asafche Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    as you can see in the example :
    http://neu.solari.co.il/gallery.html
    the gallery is working fine (try it in IE7).

    i don't know why but when i pressed un the refresh button +shift, it gives me the error message. only in IE7 (maybe also in other IE's, but i supposed to validate it only for version 7.x)

    anyone?
     
    asafche, Aug 6, 2009 IP
  5. lp1051

    lp1051 Well-Known Member

    Messages:
    163
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #5
    Hi again,
    ok, I was checking the site, and IE6 doesn't work (it tells that XMLHttpRequest is undefined). IE7 works as you say, but it is using ActiveXObject instead of XMLHttpRequest. That's why you get the error and page cannot be loaded after refresh (even basic F5). Also in your file gallery.js, which is external script, remove the first line <script type="text/javascript">. It is used to include the code into html. As a part of the JS script it generates syntax error. And I suggest again to change createRequestObject() function, or as you are using prototype on your page, you could use directly the built-in ajax handler there.
    I believe after these changes it will work fine.
    Let us know...
     
    lp1051, Aug 7, 2009 IP
  6. asafche

    asafche Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    what i did:
    var http = createRequestObject();
    function createRequestObject() {
    	var objAjax;
    	objAjax = new XMLHttpRequest();
    	return objAjax;
    }
    
    function getNewContent(){
    url="gallery/solari.txt";
    http.open('get',url);
    http.onreadystatechange = updateNewContent;
    http.send(null);
    return false;
    }
    
    function updateNewContent(){
    	if(http.readyState == 4){
    		x = http.responseText;
    		var gallery=x.split("#");
    		counter=(gallery.length-1);
    		for (i=1; i<=counter; i+=1) {
    			varim=gallery[i].split("|");
    			document.getElementById('main_content').innerHTML += "<a href='gallery/"+varim[0]+"' rel='lightbox[solari]' title='"+varim[2]+"'><img src='gallery/"+varim[1]+"' class='img_gal' alt='" + varim[2] + "'></a>";
    		}
    	}
    }
    
    getNewContent();
    HTML:
    it still gives me the same anoying message, after refreshing and reloading the page.
    anyone?
     
    asafche, Aug 11, 2009 IP
  7. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #7
    erm-i could be wrong but if it says shit like page cannot be displayed, operation aborted, then i have seen this before and you need to make sure that this bit:

    document.getElementById('main_content').innerHTML

    is run after domready. if your content is 'cached' it may fire earlier and before the browser has finished releasing all elements for dom manipulation. in order to prevent memory leaks and crashes, IE panics when an unexpected change to elements occurs as it is forced to release the DOM for it and rather than re-evaluating what has changed and how to deal with it, it pops up an the alert and dies.

    workarounds... you can do things like
    
    if (document.getElementById("main_content")) /// ... to see if it's available first.
    
    // better yet, stick in onload or onready or domready like so:
    window.onload = function() {
        getNewContent(); 
    };
    
    PHP:
    here is where i learnt about this :)
    http://www.clientcide.com/code-snippets/manipulating-the-dom/ie-and-operation-aborted/

    yep i was right - it is exactly what you are doing. god i am good! :D
     
    Last edited: Aug 11, 2009
    dimitar christoff, Aug 11, 2009 IP
  8. asafche

    asafche Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    solved it in the same time dimitar posted his post.

    i had intuition about it. didn't found any clues so tried for myself. i worked around it by using a detection & if the browser detected as MSIE i gave him something to think about. i used the setTimeout() function. it was a long shot.
    i couldn't give the info you gave above but it did the trick... here:

    var http = createRequestObject();
    function createRequestObject() {
    	var objAjax;
    		objAjax = new XMLHttpRequest();
    		return objAjax;
    }
    
    function getNewContent(){
    url="gallery/solari.txt";
    http.open('get',url);
    http.onreadystatechange = updateNewContent;
    http.send(null);
    return false;
    }
    
    function updateNewContent(){
    	if(http.readyState == 4){
    		x = http.responseText;
    		var gallery=x.split("#");
    		counter=(gallery.length-1);
    		for (i=1; i<=counter; i+=1) {
    			varim=gallery[i].split("|");
    			document.getElementById('main_content').innerHTML += "<a href='gallery/"+varim[0]+"' rel='lightbox[solari]' title='"+varim[2]+"'><img src='gallery/"+varim[1]+"' class='img_gal' alt='" + varim[2] + "'></a>";
    		}
    	}
    }
    
    var browserName=navigator.appName; 
     
    	if (browserName=="Microsoft Internet Explorer"){
    		setTimeout("getNewContent();", 5);
    	} 
    else getNewContent();
    
    HTML:
    thanks for the help, though. :D
    asaf
     
    asafche, Aug 11, 2009 IP