【问题标题】:jQuery.load() responds with error under Firefox, works fine under ChromejQuery.load() 在 Firefox 下响应错误,在 Chrome 下工作正常
【发布时间】:2011-07-19 13:08:47
【问题描述】:

我有一个在对话框而不是主窗口中打开页面的功能。稍微清理一下代码如下:

var baseurl = window.location.origin + '/static/docs/'

function onClickLink(event) {
  event.preventDefault();
  if ($("#dialog").length == 0) {
    setUpDialog()
  }
  var href = event.target.href;
  href = baseurl + href.substring(1 + href.lastIndexOf('/'));
  $("#dialog").load(href + ' .body', function(response, status, xhr) {
    if (status == "error") {
      window.location = event.target.href;
    } else {
      changeImageSrc();
      reStructure();
    }
  });
  $("#dialog").dialog({
    modal: true,
    title: event.target.text,
    width: 960,
    position: ['center', 100]
  });
}

此代码在 Chrome 中运行良好,但 (status == "error") 在 Firefox 下执行。似乎 Firefox 出现 404 错误,可能是加载页面的图像,或类似的东西。

任何想法如何在 Firefox 下获得 Chrome 行为? (你可以找到工作的example here

【问题讨论】:

  • 在 baseurl 上执行 alert() 以测试变量在两个浏览器上的值是否相同
  • window.location.origin 未定义。

标签: javascript jquery ajax google-chrome firefox


【解决方案1】:

why firefox doesn't support window.location.origin(不标准)

tl;博士

有时你需要这个而不是之前选择的答案:

var $window_location_origin = window.location.protocol+'//'+window.location.host;

解释

我需要得到window.location.origin aka window.location.protocol+'//'+window.location.host 的长度。简单地用后者替换前者是行不通的。

window.location.protocol+'//'+window.location.host.length 将返回类似http://25 的内容,这是协议和window.location.host 的长度连接在末尾。

我通过创建一个变量来解决这个问题,如下所示:

var $window_location_origin = window.location.protocol+'//'+window.location.host;

之后,我可以得到 $window_location_origin 的长度,即原来的 25 (window.location.host.length) 加上来自 window.location.protocol+'//' 的 7,得到所需的 32。

【讨论】:

  • 我完全不明白你为什么不直接使用(window.location.protocol+'//'+window.location.host).length
  • ...因为我是个假人。我相信我后来出于某种原因需要这个变量。也许有时间我会再去看看。谢谢。
【解决方案2】:
  1. 在 FireFox 中,window.location.origin 是 undefined。因此,FireFox 厌倦了获取该页面:

    http://openerp.co.hu/hu/funkcionalis-bemutato/undefined/static/docs/sales.html

    失败了

  2. 在 chrome 中,window.location.origin http://openerp.co.hu。 Chrome 绑定获取页面:

    http://openerp.co.hu/static/docs/sales.html

    成功了

不要依赖window.location.origin,尝试使用:

window.location.protocol + "//" + window.location.host

【讨论】:

  • 或者继续依赖 location.originif(location.origin!=undefined) location.origin = location.protocol + "//" + location.host
【解决方案3】:

404 表示“页面未找到”。

设置断点并检查导致问题的 URL。真的有效吗?

对于 URL 中的非法字符,Chrome 可能比 Firefox 或类似的东西更宽松。尝试将 URL 粘贴到两个浏览器的地址栏中,看看会得到什么。

【讨论】:

    【解决方案4】:

    有什么特别的错误信息吗?此外,请使用以下代码更新您的代码:

    var baseurl = window.location.origin  + '/static/docs/';
    
    function onClickLink(event) {
        event.preventDefault();
    
        if($("#dialog").length==0) {
            setUpDialog();
        }
    
        var href = event.target.href;
    
        href = baseurl + href.substring(1+href.lastIndexOf('/'));
    
        $("#dialog").load(href + ' .body', function(response, status, xhr) {
          if (status == "error") {
            window.location = event.target.href;
          } else {
            changeImageSrc();
            reStructure();
          }
        });
    
        $("#dialog").dialog({
            modal:true, 
            title:event.target.text,
            width: 960,
            position: ['center', 100]
        });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多