简体   繁体   中英

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.

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);

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM