user2777052
user2777052

Reputation: 155

remove first few characters in URL hash string

I'm trying to figure out how to remove a certain part of a URL string, as in:

if (window.location.hash == '#super-super-product') { 
    change.window.location.hash.to.this: #product  // pseudo code, obviously
}

So, to remove "super-super-", which is the first 12 chars, and keep the rest, whatever it happens to be.

The following attempts produce no change whatsoever:

if (/^#checkout-counter-./.test(window.location.hash)){ // this works perfectly
    window.location.hash.substring(0, 11); // this does nothing
    window.location.hash.substr(1, 12);  // nothing
    window.location.hash.slice(0, 11);  // still nothing
}

Thanks.

Upvotes: 2

Views: 595

Answers (2)

dee-see
dee-see

Reputation: 24078

Calling substring or any other similar methods will only evaluate the function and return it without having any effects whatsoever. You need to assign the result to the window's hash.

window.location.hash = window.location.hash.substring(0, 11);

Upvotes: 3

alex
alex

Reputation: 490133

You need to re-assign it. Otherwise the result is simply thrown away, as it's not assigned to anywhere meaningful.

window.location.hash = window.location.hash.substring(0, 11);

Upvotes: 2

Related Questions