简体   繁体   English

jQuery.load()在Firefox下响应错误,在Chrome下运行正常

[英]jQuery.load() responds with error under Firefox, works fine under Chrome

I have a function to open a page in a dialog box instead of the main window. 我有一个功能,在对话框而不是主窗口中打开一个页面。 A bit cleaned up code is the following: 有点清理的代码如下:

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]
  });
}

This code works fine in Chrome, but the (status == "error") is executed under Firefox. 此代码在Chrome中运行良好,但(状态==“错误”)在Firefox下执行。 Seemingly there is a 404 error for Firefox, might be an image of the loaded page, or something similar. 看起来Firefox有404错误,可能是加载页面的图像,或者类似的东西。

Any ideas how to get the Chrome behavior under Firefox too? 有关如何在Firefox下获得Chrome行为的任何想法吗? (you can find a working example here ) (你可以在这里找到一个有效的例子

  1. In FireFox, window.location.origin is undefined . 在FireFox中,window.location.origin undefined FireFox therefore tires to get the page: FireFox因此厌倦了获取页面:

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

    and fails 并失败

  2. In chrome, window.location.origin http://openerp.co.hu . 在chrome中,window.location.origin http://openerp.co.hu Chrome ties to get the page: Chrome绑定以获取该页面:

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

    and succeeds 并成功

Instead of relying on window.location.origin , try using: 而不是依赖于window.location.origin ,尝试使用:

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

why firefox doesn't support window.location.origin (it's not standard) 为什么firefox不支持window.location.origin (它不是标准的)

tl;dr TL;博士

sometimes you need this instead of the previously selected answer: 有时你需要这个而不是之前选择的答案:

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

explanation 说明

I need to get the length of window.location.origin aka window.location.protocol+'//'+window.location.host . 我需要得到window.location.origin的长度,也就是window.location.protocol+'//'+window.location.host Simply replacing the former with the latter, doesn't work. 简单地用后者替换前者是行不通的。

window.location.protocol+'//'+window.location.host.length will return something like http://25 , which is the protocol and the length of window.location.host concatenated on the end. window.location.protocol+'//'+window.location.host.length将返回类似http://25 ,这是协议和最后连接的window.location.host的长度。

I got around this by making a variable, like so: 我通过制作变量来解决这个问题,如下所示:

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

After that, I could get the length of $window_location_origin which would be that original 25 ( window.location.host.length ) plus the 7 from window.location.protocol+'//' , giving me the desired 32. 之后,我可以得到$window_location_origin的长度,它是原始的25( window.location.host.length )加上来自window.location.protocol+'//'的7,给我所需的32。

Any error message in particular? 特别是任何错误消息? Also, update your code with the ones below: 另外,使用以下代码更新您的代码:

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]
    });
}

404 means "page not found". 404表示“找不到页面”。

Set a breakpoint and check the URL which causes the problem. 设置断点并检查导致问题的URL。 Is it really valid? 它真的有效吗?

Maybe Chrome is more lenient when it comes to illegal characters in the URL than Firefox or something like that. 也许Chrome在URL中的非法字符比Firefox或类似的东西更宽松。 Try to paste the URL into a location bar in both browsers to see what you get. 尝试将网址粘贴到两个浏览器的位置栏中,以查看您获得的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM