I'm currently stumped on this. I've snooped around for a bit and haven't found any concrete answer using just JS to do this, and so my question is thus. If I am navigating multiple pages and want to keep query strings from the one before attached to the url, what is the easiest way to do this?
Asked
Active
Viewed 603 times
1
-
1You can use cookies.Refer http://stackoverflow.com/questions/1253821/persist-javascript-variables-between-get-requests – Ahamed Mustafa M May 24 '12 at 20:04
-
I was considering it, but if someone clears their cache it will lose all of their data thus far. – Charles Lillo May 24 '12 at 20:13
-
@CharlesLillo: Thats what's clearing cache is intended to do... Query strings are not made for data transferring, what do you trying to achieve? – Bergi May 24 '12 at 20:23
-
I am aware of this... was just wondering if I was overlooking an easy way to do it =/. A post-get method would work, but I don't know if I can use php in what I am doing. I am working on a mobile app using jquery mobile, and am storing data between pages as a user nests through. The quickest way in my mind was query strings but that really isn't for nesting. – Charles Lillo May 24 '12 at 20:29
-
1Welcome to Stack Overflow! Remember to upvote ALL useful answers, including those to others' questions. And "check" (accept) the answer that best solves your own questions. – Larry K May 24 '12 at 20:49
3 Answers
0
I don't think there is an easy way. You will have to take in account the current query parameters every time you compose a URL or create a form.

lanzz
- 42,060
- 10
- 89
- 98
-
The only way I can think of is writing a script that grabs the next query string and appends it with the last, continuously doing so. – Charles Lillo May 24 '12 at 20:05
-
-
Doing what @LarryK said in a sense, but I was trying to avoid that. – Charles Lillo May 24 '12 at 20:19
0
Are you asking for this one?
var url1 = "...", url2 = "...";
var query1 = url1.split("#")[0].split("?").slice(1).join("?");
if (!query1)
return url2;
var parts2 = url2.split("#");
parts2[0] += (parts2[0].indexOf("?")>-1 ? "&" : "?" ) + query1;
return parts2.join("#");
This extracts the query string from url1
and appends it to the query string of url2
, returning the new url2
.

Bergi
- 630,263
- 148
- 957
- 1,375
0
You can do this if the way the user "navigates" is by using links within the pages.
In a given html page, Javascript running within the page can see the url's query parameters via the
window.search
property. Mozilla docs.Then use JS to modify all of the page's anchor elements' href links to add on the already existing query parameters to the links.
Result: clicking on a link in the page will result in the new page having both the existing and new query parameters.

Larry K
- 47,808
- 15
- 87
- 140
-
This is what I initially thought. If there is no easier way, I might just end up doing this. I have a feeling this is really the only way though. – Charles Lillo May 24 '12 at 20:22
-
1Your only choices are this, cookies, or maintaining state on the server. Note that people can (and do) edit the urls. Also, there is a limit to url / query parameter lengths. – Larry K May 24 '12 at 20:51