2011-03-21 50 views
3

我有一个函数可以在对话框而不是主窗口中打开一个页面。清理代码有点如下:jQuery.load()在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] 
    });  
} 

此代码在Chrome中工作正常,但(状态==“错误”)在火狐下执行。看起来Firefox有一个404错误,可能是加载页面的图像,或类似的东西。

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

+0

在baseurl上执行alert()以测试变量在两个浏览器上的值是否相同 – Curt 2011-03-21 09:43:43

+0

'window.location.origin'未定义。 – 2011-03-21 10:07:58

回答

10
  1. 在FireFox中,window.location.origin是undefined。火狐因此轮胎让页面:

    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 
+0

或改为继续依靠'location.origin'与'if(location.origin!= undefined)location.origin = location.protocol +“//”+ location.host' – EaterOfCode 2013-03-18 22:00:29

0

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

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

404表示“未找到页面”。

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

也许Chrome对于URL中的非法字符比Firefox更加宽松。尝试将URL粘贴到两个浏览器中的地址栏中,以查看您获得的内容。

1

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

t1; dr

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

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

解释

我需要得到的window.location.origin长度又名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.

+0

我不明白为什么你不只是使用'(window.location.protocol +'//'+ window.location.host).length'? – EaterOfCode 2013-03-15 17:00:12

+1

...因为我是个假人。我相信我之后会因为某种原因需要这个变量。也许我会在有空的时候重温。谢谢。 – curtisblackwell 2013-03-16 02:06:16

+0

哈哈大家都在开始;) – EaterOfCode 2013-03-18 21:58:44

相关问题