简体   繁体   中英

Override location.search javascript without reloading page

I want to modify the location.search attribute, right now if I do:

<script>location.search = "dummyparam=dummyvalue";</script>

But it realoads the page to the new url including the new values of location.search, what I want is to fake it.

I tried using:

Object.defineProperty(window, 'location', {
  search: "fakeparam=fakevalue"
});

Also doing:

Object.defineProperty(location, 'search', {
  search: "fakeparam=fakevalue"
});

But none worked, any clues?

Thanks!

The only way you can modify the url (including location.search) without reloading is using HTML5 history API push state.

You can do something like:

window.history.pushState("", "", "/this-is-new-url?fakeparam=fakevalue");

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