javascript changing the get parameter without redi

2019-02-09 04:44发布

问题:

This question already has an answer here:

  • How can I change the page URL without refreshing the page? 4 answers

How can I just change the get parameter without redirecting?

parent.location.search = "?after=20"; 
// ok that changes, but also redirect to the new page

Any solution? Or answer is no, if its no, please write big no.

回答1:

Update

Since this is the accepted answer, and no longer true, refer to this duplicate question for up to date information.

Original answer follows:


NO

You can come close with the anchor portion of the URL which is accessible by the hash property of the location object

parent.location.hash = "whatever value you want";


回答2:

If your aim is to use the query string to store some state that can later be restored from a bookmark, you should use anchors instead.

However, if you must change the query string for some reason, actually there is a way. However, I don't endorse this. I'm just mentioning it for completeness.

When a server returns a 204 No Content response, most browsers won't do anything -- i.e. won't even attempt to transition to another page or even wipe the current page. What you can do is to make the backend just emit a 204 response when a request is made to the same page that was just served, with a change in the query parameters.



回答3:

By now the answer is YES

At least in HTML5, which means all major browsers (IE10+)
Does Internet Explorer support pushState and replaceState?
http://caniuse.com/#feat=history

You can also check my answer on another SO question:
How can I change the page URL without refreshing the page?



回答4:

Uh, no - if you change the URL parameter, your browser will load the new page.



回答5:

You cannot alter the querystring (ie. the part that stars with the ?) without reloading the page from the server. You can however use page anchors like http://www.example.com/page.html#anchorname to affect the url without reloading a page from the server.



回答6:

You can't do this because it would be a security risk if you could.... If pages could change the value in the URL bar, it would be easy for page put up by a phisher to change that value to appear to be a page on your bank's website.