2

I have a simple redirect in my jQuery script. You click a link, it performs an asynchronous save, then sends the user to the next page via window.location.href. This works fine in all browsers except I have an issue in IE (surprise surprise). On one page in IE, I get the following error when trying to run the script

E.location.protocol is null or not an object

What is odd, is that the script works on other pages. the only thing I can see different is that the page it breaks on contains a google map, whereas the others do not.

Also, this seems to be a problem only in later versions of jQuery (1.4+) but I have to use that for other functionality.

Any suggestions? Thank you.

The save, redirect script ( which gets its location from the 'goto' attribute in an anchor tag in the page):

$("#save_and_go_button").click(function(){
  showAction('Saving...');         
  $.ajax({
    type: "POST",  
    url: "/admin_and_tools/async/save.php",
    data: $("#main_form").serialize(),
    dataType: "html",
    success: function(results){
    if(results == 'success'){
     hideAction(); 
     //alert('The record has been saved.');
     document.location.href = $("#save_and_go_button").attr('goto');
    }else{
     alert('failed' + results);
    }
    }
  });        
 });

EDIT: Here is the line it does on in the jQuery code:

    if(E.location.protocol!=="file:")
    try{return new E.XMLHttpRequest}catch(a){}try{
return new E.ActiveXObject("Microsoft.XMLHTTP")}catch(b){}};
dmgig
  • 4,400
  • 5
  • 36
  • 47
  • just for info, you're accessing `document.location.href` (should work anyway, just in case) – jAndy Jan 27 '11 at 19:51
  • "document.location.href " is not EXACTLY the same as "window.location.href" - document... is supposed to be read only but some browsers do not honor that. – Mark Schultheiss Jan 27 '11 at 19:52

3 Answers3

1

Instead of

document.location.href = $("#save_and_go_button").attr('goto');

Use

window.location.href = $("#save_and_go_button").attr('goto');

From what I understand document.location is read-only.

document.location was originally a read-only property, although Gecko browsers allow you to assign to it as well. For cross-browser safety, use window.location instead.

Josiah Ruddell
  • 29,697
  • 8
  • 65
  • 67
  • I wish I could say that worked, but I still get the same error. It dies within jQuery 1.4.4, at the following line (i will edit my question to include this): if(E.location.protocol!=="file:")try{return new E.XMLHttpRequest}catch(a){}try{return new E.ActiveXObject("Microsoft.XMLHTTP")}catch(b){}}; – dmgig Jan 27 '11 at 20:11
1

Change document.location.href to window.location.href

This is an old issue with IE that crops up sometimes :)

Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100
  • unfortunately, I get the same error here. It is in a line of code (added to question) in the jQuery source, that specifically deals with ActiveX content. – dmgig Jan 27 '11 at 20:14
0

Well, what I've done as a work around is to drop this from the jQuery 1.4.4 source

/*
 * Create the request object; Microsoft failed to properly
 * implement the XMLHttpRequest in IE7 (can't request local files),
 * so we use the ActiveXObject when it is available
 * Additionally XMLHttpRequest can be disabled in IE7/IE8 so
 * we need a fallback.
 */

if ( window.ActiveXObject ) {
    jQuery.ajaxSettings.xhr = function() {
        if ( window.location.protocol !== "file:" ) {
            try {
                return new window.XMLHttpRequest();
            } catch(xhrError) {}
        }

        try {
            return new window.ActiveXObject("Microsoft.XMLHTTP");
        } catch(activeError) {}
    };
}

It seems to work now. Although now it def doesn't like document.location.href at all , and only goes on window.location.href. So I don't know. This will probably crash some other part of my application.

Will keep this updated. UPDATE - so far, so good.

dmgig
  • 4,400
  • 5
  • 36
  • 47
  • 1
    Allow me to follow up here. I am an idiot. What I have finally found, months after the fact, is that I had a script that I was redeclaring var location as a global, destroying the original location variable. It was in my google maps initialization script, so that's how I figured google maps was screwing things up. Thank you to JSLint for helping me find this error. – dmgig Apr 11 '11 at 16:36