11

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.

Jeremy Stein
  • 17,438
  • 15
  • 64
  • 80
Basit
  • 13,920
  • 29
  • 88
  • 145
  • ajax purpose, to keep ajax working fine and let user share the link with other if they want to – Basit Sep 28 '09 at 17:17
  • 3
    The standard paradigm with AJAX is to use the anchor instead of the query string. I'd go with the flow. – Ates Goral Sep 28 '09 at 17:23
  • ya but thats extra work to detect value in anchor and load it and if javascript was disable by user, then you have to also render url and see if any variable is in the anchor tag, if it is, then assign it. this way it will be available for javascript or without javascript. in other words its lots of extra work lol. – Basit Sep 28 '09 at 18:28
  • If JS is disabled, how are you changing either the query string or the anchor in the first place? Perhaps you should explain to use what it is that you're exactly trying to achieve. – Ates Goral Sep 28 '09 at 18:55
  • like if i share a link with you www.domain.com/#after=20.. javascript can render it and display, but rather then let javascript rander, i would just let php render intead of javascript and display. – Basit Sep 28 '09 at 19:17
  • So, you're willing to implement the same rendering logic both on the client side using AJAX and on the server side? Isn't it a bit overkill and perhaps an unnecessary optimization? Why are you reducing the client's work? It's better to reduce the server load. Just do as much as you can on the client side. – Ates Goral Sep 28 '09 at 19:57
  • yes you are right, but sometimes for somethings you have to do both, that would be a proper way of doing it. you cant just let go everything on js, i prefer both. specially because of search engines! – Basit Oct 28 '09 at 01:08

6 Answers6

11

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";
Community
  • 1
  • 1
Peter Bailey
  • 101,481
  • 30
  • 175
  • 199
  • 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 Oct 27 '09 at 21:52
  • 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 Mar 25 '10 at 03:03
6

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
  • 126,894
  • 24
  • 129
  • 188
  • 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 Sep 28 '09 at 17:21
  • You can safely assume that all reasonably modern browsers behave the same way -- ie. stay on the same page. – Ates Goral Sep 28 '09 at 17:22
  • have you tested on latest browsers? – Basit Sep 28 '09 at 18:29
  • 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 Sep 28 '09 at 18:53
  • 1
    Tried this in Safari 4, it clears the query string when it receives the 204 response. – eyelidlessness Oct 27 '09 at 17:41
4

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?

Community
  • 1
  • 1
roberkules
  • 6,389
  • 1
  • 40
  • 51
1

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

user97410
  • 726
  • 1
  • 6
  • 22
1

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
  • 147,774
  • 24
  • 184
  • 187
0

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
  • 29,764
  • 13
  • 43
  • 57
  • 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 Oct 27 '09 at 17:47
  • 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 Oct 27 '09 at 21:53
  • 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 Oct 27 '09 at 22:39
  • 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 Oct 28 '09 at 01:12