javascript - jQuery.load() 在 Firefox 下响应错误,在 Chrome 下工作正常

标签 javascript jquery ajax google-chrome firefox

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

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 )

最佳答案

  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

关于javascript - jQuery.load() 在 Firefox 下响应错误,在 Chrome 下工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5375788/

相关文章:

javascript - 有没有办法删除数据层中的对象?

来自 ajax 回调的 Javascript(原型(prototype))方法调用

php - 我想使用 AJAX 根据单击的链接的 ID 动态生成布局

javascript - 如何加载外部 html 文件?

Javascript "If"语句或 "&&"逻辑运算符

javascript - 如何在数据库中存储 Bootstrap 行和列

javascript - onchange 之后的 Ajax

javascript - 如何在 React 应用程序(CRA)中从 jQuery 加载 $ 函数?

javascript - FancyBox2 导致 Firefox 13 在滚动条出现时移动一个 div

asp.net - asp.net UpdatePanel 中的 jquery ui datepicker