Extracting QueryStrings with JavaScript
Last Post 10/25/2010 9:00 AM by Joseph Craig. 1 Replies.
Author Messages
Dave Hassall
Nuke Pro
Nuke Pro
Posts:86


--
10/25/2010 6:41 AM  
Hi

I have been successfully using the QueryString feature to dynamically affect the content of my page when visitors come from Google Adwords.

e.g. a search for Driving Lessons Creekmoor and clicking on the Ad take the user to

www.newdriverschools.co.uk/?Town=Creekmoor&PostCode=BH

The page uses DataSprings Dynamic Forms and is brilliant. It extracts the QueryString for me and puts it in each field.

But times have moved on and I want to be more dynamic with the QueryString.

I have searched for examples and come across many....

They are all very powerful but pretty complex to unravel.

Does anyone have a, simple, example that shows how to extract a 1st, 2nd and 3rd string without be super powerful to extract 1 - n strings?

My requirememt is to have just 3 items "Town", PostCode" and "Price" Thanks

Dave
Joseph Craig
DNN MVP
Posts:11667


--
10/25/2010 9:00 AM  
The "modern" way to do this is by using jQuery, a powerful scripting tool that builds on javascript.  I quick Googling  shows quite a few examples.  I found the following on stackoverflow.com.  I didn't test it, but it looks like a good way to go:

Roshambo on snipplr.com has a really hott&simple script to achieve this. With his script you also get to pull out just the prams you want easily:

http://snipplr.com/view/26662/get-u...-improved/

Here's the gist:

$.urlParam = function(name){
var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
if (!results) { return 0; }
return results[1] || 0;}

Then just get your parameters from the query string.

So if the url/query string was xyz.com/index.html?lang=de

just call var langval = $.urlParam('lang'); and you've got it

UZBEKJON has a great blog post on this as well:
http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html


Joe Craig, Patapsco Research Group
Complete DNN Support


---