views:

919

answers:

5

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  A: 

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

Not necessarily... If the server returns a 204, the browser will stay on the same page.
Ates Goral
+1  A: 

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.

Asaph
+7  A: 

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";
Peter Bailey
i like the @ates-goral answer better, but its not supported by all browsers, very sadly. plus it wont be secruity risk, if someone changes the query string, its not like person is changing the complete path or domain. even if user can change the path, that dont mean its a security risk, because domain would still be same, unless its a blog site.
Basit
I like this, but you need to be aware that the # value is never sent to the server http://stackoverflow.com/questions/774136/retrieving-anchor-link-in-url-for-asp-net
row1
+3  A: 

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.

Ates Goral
so this method really depend on a browser, if browser do redirect, then there is no way stopping it.. right? i can set 204 response, only if i know the draw-backs
Basit
You can safely assume that all reasonably modern browsers behave the same way -- ie. stay on the same page.
Ates Goral
have you tested on latest browsers?
Basit
I remember getting it to work on both Firefox and IE. You should definitely test it yourself to be sure. But again, I implore you to use anchors instead! :)
Ates Goral
Tried this in Safari 4, it clears the query string when it receives the 204 response.
eyelidlessness
A: 

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.

Spike Williams
There's a big difference between changing the query string and changing the rest of the path. If your current host name isn't fooling anyone, it's not going to fool anyone any more when you change it, especially because you can still change it and control what's output even if it does go to the server to retrieve the new URL.
eyelidlessness
like i said below:i like the @ates-goral answer better, but its not supported by all browsers, very sadly. plus it wont be secruity risk, if someone changes the query string, its not like person is changing the complete path or domain. even if user can change the path, that dont mean its a security risk, because domain would still be same, unless its a blog site.
Basit
I don't really think it should be up to the browser to decide what parts of the URL in the address bar its safe to change or not...that's kind of a silly minor feature for a browser to implement, and you know that it would be different between different browsers.
Spike Williams
ya i agree, but browser should let users change the path (NOT DOMAINS), cause now days everyone prefer to do stuff using ajax and changing path is for just letting user use to share the link with other users for current page. yes i know you can do it with hash too, but its confusing for people to share link with title of different media, but they are sharing of different page, because they got that from ajax.. obviously there are some other solutions.. but ideal would be letting the domain owners change the path they own.
Basit