Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript load()在Firefox下响应错误,在Chrome下工作正常_Javascript_Jquery_Ajax_Google Chrome_Firefox - Fatal编程技术网

Javascript load()在Firefox下响应错误,在Chrome下工作正常

Javascript load()在Firefox下响应错误,在Chrome下工作正常,javascript,jquery,ajax,google-chrome,firefox,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 = ba

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

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的行为吗?
(您可以找到一个工作模式)

是否有任何特别的错误消息?此外,请使用以下代码更新您的代码:

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表示“未找到页面”

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

对于URL中的非法字符,Chrome可能比Firefox之类的浏览器更宽容。尝试将URL粘贴到两个浏览器中的位置栏中,以查看得到的内容

  • 在FireFox中,window.location.origin是
    未定义的
    。因此,FireFox厌倦了获取页面:

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

    失败了

  • 在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
    
    (这不是标准的)

    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\u location\u origin
    的长度,这将是原始的25(
    window.location.host.length
    )加上
    window.location.protocol+'/'
    中的7,给我所需的32。

    执行警报()在baseurl上测试变量的值在两个浏览器上都是相同的
    window.location.origin
    未定义。我完全不明白你的意思,为什么你不直接使用
    (window.location.protocol+'/'+window.location.host)。length
    ?…,因为我是个哑巴。我相信出于某种原因,我需要这个变量。也许我有时间再去看看。谢谢。或者继续依赖
    location.origin
    if(location.origin!=未定义)location.origin=location.protocol+“/”+location.host