簡體   English   中英

location.href未定義

[英]location.href is undefined

我目前無法以任何可能的方式讀取Firefox(v 33.1)中當前瀏覽器的位置。 如果被jQuery的“就緒”功能讀取,則location.href,window.location.href和document.location.href為“未定義”。 .hash等也是如此。但是,在控制台中,它們將完全返回應有的狀態。

$(function(){$("a").unbind("click").click($S.r);$S.r();});
$S={
    r: function()
    {
        try{$I=((typeof this.href)?this.href:location.href).replace(/^.*?#/,'');}catch($E){console.error($E);setTimeout(function(){$S.r();},250);return false;}
        $S.c($I);
    },
    c: function($I)
    {
        $("content").css({"opacity":"0"});
        setTimeout(function()
        {
            $("content spacer").html($S.h($I));
            $("content").css({"opacity":"1"});
        },1000);
    },
    h: function($I)
    {
        switch ($I)
        {
            case "impressum": return "Impressum";
            case "email": return "E-Mail";
            case "mitglieder": return "Familien-Mitglieder";
            default: return "Startseite";
        }
    }
};

網址: http//saris.it

我要實現的目標:/#test將觸發腳本以加載Test的內容。單擊鏈接時此方法有效。 在頁面加載時,它不是。 相應的變量被填充在控制台中。 它們不在jQuery中。 使用setTimeout根本沒有幫助。

我應該如何解決這個問題?

錯誤在這里:

((typeof this.href)?this.href:location.href).replace(/^.*?#/,'')

嘗試:

((typeof this.href != 'undefined')?this.href:location.href).replace(/^.*?#/,'')

未定義的不是location.href ,而是this.href
因為typeof this.href是字符串'undefined' ,所以在比較時為true。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM