Himmators
Himmators

Reputation: 15016

What is the conditional for location.hash on the page root?

I tried the following:

    console.log(location.hash)
    if(location.hash = ''){
        console.log('home')
    }

What conditional should I set to get the console to log home on for example example.com/

Upvotes: 0

Views: 50

Answers (3)

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167182

The location.hash will not be set:

if (location.hash == null)
if (!location.hash)

And in your code, you are assigning! Not comparing! Use ==!

Upvotes: 0

JGDarvell
JGDarvell

Reputation: 214

The following will log home on example.com/

if(window.location.pathname == '/'){
    console.log('home')
}

You need to use the pathname property, not hash.

Upvotes: 1

Richard Foster
Richard Foster

Reputation: 608

Are you looking to test if a hash exists?

// If there is no hash
if(!window.location.hash){
    console.log("no hash");
}

Upvotes: 0

Related Questions