Sometimes it can be very useful to retrieve query string values on the client-side in Javascript. This is not all that common, but occasionally I will have a simple popup window (simple enough to be unworthy of an aspx file extension) that I need to pass a query string to it and have Javascript react to it accordingly. If there is no server-side processing, then there is no reason to have ASP.NET serve the page. It wouldn't be worth it just to gain access to the Request.QueryString collection. Rather, it would be more efficient to use Javascript to retrieve the query strings. For this purpose, I have written up a function that makes this task as simple as with ASP.NET...
function getQueryStrings() { var argList = new Object(); if(window.location != null && window.location.search.length > 1) { var urlParms = window.location.search.substring(1); var argPairs = urlParms.split('&'); for(var i = 0; i < argPairs.length; i++) { var pos = argPairs[i].indexOf('=') if(pos == -1) continue; else { var argName = argPairs[i].substring(0, pos); var argVal = argPairs[i].substring(pos + 1); if(argVal.indexOf('+') != -1) argVal = argVal.replace(/\+/g, ' '); argList[argName] = unescape(argVal); } } } return argList; }
The function will return an object containing each query strings name/value pair (if it finds any). Here is an example of how to use the function, it should cover all practical uses...
// Given that the URL is page.htm?foo=bar window.onload = function() { var queryStrings = getQueryStrings(); // These two statements are identical, // both will show "bar" in the alert alert(queryStrings.foo); alert(queryStrings['foo']); // Check if a query string exists if(queryStrings.foo != null) { document.write(queryStrings.foo); } // Loop through all query strings for(var prop in queryStrings) { alert(prop + ': ' + queryStrings[prop]); } }
That's all folks! You just might need to use it someday. Best regards...
I was always surprised that JS didn't support this "out of the box", as the saying goes.
Unfortunately, I am going to have to say No. You really should upgrade your browser so you can truly experience the better things on the web. You are literally missing out.
I know what you are thinking... who is this asshole telling me that I must upgrade my browser. Well, quite frankly, I am probably the same way as you are. I stick with software I am familiar with. If they release something new, I say "so what". If the new and improved version does not provide any obvious benefit, then why should I waste my time? In fact, MSN Messenger alerted me about a month or two ago telling me I have to upgrade to continue using it. I have over 100 contacts on there and guess what... I refused. I still have not upgraded to this day and I cannot sign in. The version that I had was flawless, I don't want all the new bullshit. They say I have to upgrade, I say no thanks. Now I am on Skype, with 3 contacts. I am not communicating with all my friends, but I am making a stand and following my principles. It feels good, doesn't it?
But, what you need to think about is the significance of a web browser. You spend more time in a browser than you do any other piece of software. And it is blatantly obvious that the Internet can be a dangerous place if you have weak security. You need to be up to date. I am in no way telling you to get Firefox, Opera, etc. I am an all-time IE user and I will not change, but you really need to upgrade to IE7. Nothing much has changed, it has only gotten better. Everything you are familiar with now will work almost exactly as it does. They had some issues to begin with but it is all good now.
So do yourself a favor and go upgrade. Now.
http://www.microsoft.com/windows/downloads/ie/getitnow.mspx
Best regards...
leaving that aside ... congrats on setting up this blog, its sweet ;)
make it a project on codeplex :p
Kind regards,
Wim
Thanks! I have decided to come up with a different, less-annoying way of notifiying IE6 users. Not sure when I will get that done. The site looks much better in IE7, by the way :P
I am not going to make an open-sourced project out of my blog. I would recommend everyone to check out http://www.dotnetblogengine.net/ if they want a well-written open-source blog engine.
Thanks for stopping by!
this code is really helpful.
great work thanks
Guess What?
There are a few basic guidelines you should be aware of before leaving a comment…
- If you choose to display your email address, it will not be detected by spam bots
- Comments are limited to 3,000 characters; so far you have used none of them
- HTML will be encoded; links and line breaks will be converted automatically
- Comments containing five or more links will be subject to moderation
