Search code examples
javascriptjqueryfirefoxhref

location.href is undefined


I'm curently in trouble with any possible form of reading the current browser location in Firefox (v 33.1). location.href, window.location.href and document.location.href are "undefined", if being read by jQuery's "ready" functionality. Same for .hash etc. However, in the console they return exactly what they are supposed to.

$(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";
        }
    }
};

URL: http://saris.it

What I want to achieve: /#test shall trigger the script to load the content for Test This works when clicking a link. On pageload, it does NOT. Corresponding vars are filled in the console. They are NOT in jQuery. Using setTimeout did NOT help at all.

How should I work arround this issue?


Solution

  • Error here:

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

    Try:

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

    Undefined not location.href, but this.href,
    because your typeof this.href is string 'undefined', it's true, when comparision.