jQuery window.location.href issue in IE, null or not an object error. Could it be google maps?

13,427

Solution 1

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.

Solution 2

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

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

Solution 3

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.

Share:
13,427
dmgig
Author by

dmgig

Full Stack Developer, Domain Driven Design, DevOps. Javascript, HTML/CSS, PHP, and anything else I can get my hands on. Thanks for all your help.

Updated on June 04, 2022

Comments

  • dmgig
    dmgig almost 2 years

    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
    dmgig about 13 years
    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
    dmgig about 13 years
    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
    dmgig about 13 years
    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.