2011-03-21 49 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

相關問題