【问题标题】:jQuery window.location.href issue in IE, null or not an object error. Could it be google maps?IE 中的 jQuery window.location.href 问题,null 或不是对象错误。会不会是谷歌地图?
【发布时间】:2011-06-16 19:18:36
【问题描述】:

我的 jQuery 脚本中有一个简单的重定向。您单击一个链接,它会执行异步保存,然后通过 window.location.href 将用户发送到下一页。这在所有浏览器中都可以正常工作,除了我在 IE 中遇到问题(惊喜)。在 IE 的一页上,尝试运行脚本时出现以下错误

E.location.protocol is null or not an object

奇怪的是,该脚本可以在其他页面上运行。我唯一能看到的不同之处在于它所在的页面包含谷歌地图,而其他页面则没有。

此外,这似乎只是在更高版本的 jQuery(1.4+)中才有问题,但我必须将其用于其他功能。

有什么建议吗?谢谢。

保存、重定向脚本(从页面锚标记中的“goto”属性获取其位置):

$("#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);
    }
    }
  });        
 });

编辑:这是它在 jQuery 代码中所做的行:

    if(E.location.protocol!=="file:")
    try{return new E.XMLHttpRequest}catch(a){}try{
return new E.ActiveXObject("Microsoft.XMLHTTP")}catch(b){}};

【问题讨论】:

  • 仅供参考,您正在访问document.location.href(无论如何都应该工作,以防万一)
  • "document.location.href " 与 "window.location.href" 不完全相同 - document... 应该是只读的,但有些浏览器不支持这一点。

标签: jquery internet-explorer redirect href


【解决方案1】:

document.location.href 更改为window.location.href

这是 IE 的一个老问题,有时会出现 :)

【讨论】:

  • 不幸的是,我在这里遇到了同样的错误。它位于 jQuery 源代码中的一行代码(添加到问题中),专门处理 ActiveX 内容。
【解决方案2】:

而不是

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

使用

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

据我了解document.location is read-only

document.location 原本是一个 只读属性,虽然 Gecko 浏览器允许您将其分配为 好吧。为了跨浏览器的安全,使用 window.location 代替。

【讨论】:

  • 我希望我可以说这有效,但我仍然遇到同样的错误。它在 jQuery 1.4.4 中死亡,在以下行(我将编辑我的问题以包括此): if(E.location.protocol!=="file:")try{return new E.XMLHttpRequest}catch(a) {}try{return new E.ActiveXObject("Microsoft.XMLHTTP")}catch(b){}};
【解决方案3】:

好吧,我所做的就是从 jQuery 1.4.4 源代码中删除它

/*
 * 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) {}
    };
}

现在看来可以了。虽然现在它 def 根本不喜欢 document.location.href,而且只在 window.location.href 上运行。所以我不知道。这可能会使我的应用程序的其他部分崩溃。

将保持更新。更新 - 到目前为止,一切都很好。

【讨论】:

  • 请允许我在这里跟进。我是一个白痴。事后几个月我终于发现,我有一个脚本,我将 var location 重新声明为全局变量,破坏了原始的 location 变量。它在我的谷歌地图初始化脚本中,所以我认为谷歌地图把事情搞砸了。感谢 JSLint 帮我找到这个错误。
猜你喜欢
  • 2012-09-26
  • 2011-11-29
  • 2020-03-30
  • 2018-04-06
  • 2012-12-05
  • 2018-05-14
  • 1970-01-01
  • 1970-01-01
  • 2016-04-23
相关资源
最近更新 更多