Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if function with window.location.hash help

I have a function that changes the hash in the url and inserts/removes a div from my main page. I did this was so that I can have a page that you can maneuver through without a reload, but at the same time i wanted people to be able to bookmark a certain section and go to it later without having to go through the page again.

When i try to call my hash() function, which closes all divs and opens up the specific div depending on the hash, it doesn't work. I probably dont have the right thing in the if statements, because when I put an alert() in the hash() function, it pops up like its supposed to.

function hash(){
    if ( window.location.hash == "dcontact" ) { 
        removedivs();
        InsertContent('dcontact');
    }
    if ( window.location.hash == "dhome" ) {
       removedivs();
       InsertContent('dhome');
    }
}
hash();

I'm aware that there are probably better ways of doing everything i mentioned, but this is the only website I'm going to be making, and I couldn't care less how messy the script is in the end, as long as it works.

like image 820
pobrien Avatar asked Nov 18 '10 10:11

pobrien


1 Answers

the reason it doesn't work is the actual hash (in the US I think you call it a pound) symbol - # at the beginning of window.location.hash

From memory IE doesn't put the hash symbol on it, so do this:

function hash() {
    var hash = window.location.hash.replace('#','');

    if (hash == "dcontact"){removedivs(); InsertContent('dcontact');}
    if (hash == "dhome"){removedivs(); InsertContent('dhome');}  
}

You could also consider just calling InsertContent(hash) rather than doing an if() for every different link you'll have

like image 89
Stephen Avatar answered Nov 08 '22 17:11

Stephen