/    Sign up×
Community /Pin to ProfileBookmark

How can I get query string values?

Is there a plugin-less way of retrieving query string values via jQuery (or without)?

If so, how, and if not what plugin do you recommend?

to post a comment
JavaScript

13 Comments(s)

Copy linkTweet thisAlerts:
@Vince616Apr 22.2013 — Hi bud

It is fairly easy to get the querystring by just using javascript, if you could expand on why you need to do this it might help us give you a more relevant answer.

This script will strip the querystring and then split the name value pairs out and display them to the page.
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;script type="text/javascript"&gt;
function getQuerystring(){
var q=document.location.toString();
q=q.split("?");
q=q[1].split("&amp;");
var str=""
for(i=0;i&lt;q.length;i++){
tmp=q[i].split("=")
str+=i +". querystring name is '"+tmp[0]+"' value is '"+tmp[1]+"'&lt;br /&gt;"
}
document.getElementById("results").innerHTML=str
}
onload=function(){
getQuerystring()
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id="results"&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;


V
Copy linkTweet thisAlerts:
@007JulienApr 22.2013 — The property [I]search[/I] ([I]document.location.search[/I]) gives immediately the query portion of a URL(with the ? character). See location object
Copy linkTweet thisAlerts:
@Vince616Apr 22.2013 — The property [I]search[/I] ([I]document.location.search[/I]) gives immediately the query portion of a URL(with the ? character). See location object[/QUOTE]

That is interesting Julien, I didn&#8217;t know that

One thing that confuses me with these kind of requests regarding getting the name value pairs out of a querystring client side, is that presumably since it is your script, in your page, you already have that information server side, which kind of negates needing strip it out of the url client side in the first place.

Or am I missing something, is there a condition where this would be useful?

V
Copy linkTweet thisAlerts:
@007JulienApr 22.2013 — It's a possibility to transfer data between two pages with or without server...
Copy linkTweet thisAlerts:
@rnd_meApr 22.2013 — it can be useful for more than just passing data without a server, which is better done using localStrorage for size reasons.

many times, you might want to style a section differently depending on some GET param. Or you may want to build links to other functionality based upon what is happening right now.

while GET info is used by the server, it's often easier to deliver scripts as a bundle and let them sort themselves out without help from the server. this could mean, for example, loading an additional validation lib when a form page is offered, or a gallery script if a slideshow is presented.

there's also applications for custom error messages based on failed GET form submits, and the potential to use a querystring to store the application's state. This lets the back button change app state all at once, but requires javascript to decode and apply all those settings once the page url changes.

in short, there a lot the server can't do, especially without internet, but GET params are a common way to store and share small chunks of data independently of the domain, application, or server implementation.
Copy linkTweet thisAlerts:
@Vince616Apr 22.2013 — Hi rnd me

Sorry bud, but I just don’t buy it


it's often easier to deliver scripts as a bundle and let them sort themselves out without help from the server. this could mean, for example, loading an additional validation lib when a form page is offered, or a gallery script if a slideshow is presented.
[/QUOTE]

That seems like a really bad idea, bundling scripts and letting them sort it out with the querystring, that sounds like a recipe for disaster to me, and a lot more work. Personally I would just make the page and the scripts that go with it and send them at the same time or make a library and send it to all the pages if I was going to use the scripts in all the pages, but this negates your point.


there's also applications for custom error messages based on failed GET form submits,
[/QUOTE]

If you are using GET to submit a form (which I don’t do) and it fails then you are still server side so you can send whatever error message you want, again it seems rather pointless to write a script to do what the server can do in a heartbeat.


and the potential to use a querystring to store the application's state. This lets the back button change app state all at once,
[/QUOTE]

Since the web is, to all intents and purposes is a stateless environment, with the exception of session variables which are, as I am sure you know, kept on the server. You cannot keep with any degree of accuracy, your applications state using the querystring. The sad fact is that kind of coding would leave you wide open to someone hacking your app


in short, there a lot the server can't do, especially without internet,
[/QUOTE]

err..... well there is nothing a server can do without the internet it seems rather silly to have to point that out


but GET params are a common way to store and share small chunks of data independently of the domain, application, or server implementation.
[/QUOTE]

Again I don’t buy it, if you wanted to keep small chunks of data then use a cookie since that was what they were designed for in the first place.

I am still fairly sure that there are several better ways of doing all of what you have mentioned, you have even alluded to one yourself (HTML5’s localstorage), and are to my mind at least, far more secure that stripping out the name value pairs from the querystring, it is just far to visible to the user and far too easy to be manipulated

In short I remain unconvinced that this is a good approach, or even useful.

V
Copy linkTweet thisAlerts:
@007JulienApr 22.2013 — A simple example : to retrieve a post on a page, it could be useful to send and return the scroll height in a query string...
Copy linkTweet thisAlerts:
@rnd_meApr 22.2013 — Hi rnd me

Sorry bud, but I just don&#8217;t buy it

In short I remain unconvinced that this is a good approach, or even useful.


V[/QUOTE]


i never said any of the examples were the best or even a good approach, i was simply trying to give you some basis for what this sort of functionality MIGHT be used for in the wild.

that said, what's up with the multiquote refutation, kinda rude...


The fact the JS doesn't ship with the parser is a good indication that it's not the go-to IO paradigm for 21st century computing.

a lot of app don't have or need a server, but they still want outside linking, and this is one obvious way of doing so.

personally, i like location.hash better, since it doesn't go over the wire, but it both let you create incoming links the prep the display or pre-fill some data in the app.

if you have a blank slate in front of you, then yes, there are better approaches to each of the examples i mentioned.

often times, we don't control what we're handed, we just have to make it work.

it would be nice if every useful querystring value were poked into html in some semantic fashion, but that's not always the case.
Copy linkTweet thisAlerts:
@Vince616Apr 23.2013 — I am terribly sorry that I have managed to offended you rnd me, that was not my intention, I just saw holes in your argument and thought that it was worth replying to them in a logical and systematic manner which involved, as you put it a &#8220;multiquote refutation&#8221;, I will from this point on refrain from refuting your arguments
Copy linkTweet thisAlerts:
@Vince616Apr 23.2013 — A simple example : to retrieve a post on a page, it could be useful to send and return the scroll height in a query string...[/QUOTE]
I see your point, but, lets say you append the querystring of some forum page with something like &#8220;&sh=464&#8221; to give you the value of the scroll height, if you want to retrieve that value client side you will need to write a function to parse it out of the querystring then write another function to scroll the page to the value: whereas if you were to do it server side all you would need to do is write the one function to scroll the page and add the value server side, surely that is far easier.
Copy linkTweet thisAlerts:
@rnd_meApr 23.2013 — I see your point, but, lets say you append the querystring of some forum page with something like &#8220;&sh=464&#8221; to give you the value of the scroll height, if you want to retrieve that value client side you will need to write a function to parse it out of the querystring then write another function to scroll the page to the value: whereas if you were to do it server side all you would need to do is write the one function to scroll the page and add the value server side, surely that is far easier.[/QUOTE]
actually it's more complicated to do it like that, correctly at least. Typically data, view and content are separated, which means javascript should be served from external files instead of inline scripts. This means you shouldn't tuck the scroll amount into the html view, which means it comes from a 2nd url. for most web systems like lamp and asp, that adds additional complexity. and ok, so you separate the state from the html view, that good, but how to you get the scroll amount to the client on an external url? then you have to use ajax or create a callback to use jsonp. that's a lot more complicated than 1 drop-in function and 1 custom line of js.

i don't get why anyone would want to use a server for something that can be done on the client. it's not 1995 anymore.

it's quicker for a million folks to parse their own params than for your server to parse them all.

think about parallel processing versus serial processing, and which is faster...

let them do the work for free and use your server CPU cycles where it counts.
Copy linkTweet thisAlerts:
@Vince616Apr 23.2013 — Hi rnd me,

As I mentioned before I will refrain from refuting your arguments, as I have neither the time, nor the desire to upset you.

Let us agree to disagree and leave it at that.
Copy linkTweet thisAlerts:
@rnd_meApr 23.2013 — Hi rnd me,

As I mentioned before I will refrain from refuting your arguments, as I have neither the time, nor the desire to upset you.

Let us agree to disagree and leave it at that.[/QUOTE]

i;m glad you see the light, who needs that dusty old server anyway?
×

Success!

Help @steve007 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...