View Full Version : Accessing the query string.

05-30-2012, 03:52 PM
I have a some javascript that imbeds a survey into a webpage I created. In the script is a field which allows you to define pre-filled values:


The customer wants to give the person filling out the form a URL that has "field816=resIDnum" in it. The only problem is the survey program does not allow me to use URL appends. I need to use the javascipt.

I want to make a script that automatically fills the 'defaultValues' field in the JavaScript.

I was thinking about telling the customer to make there URL's in this format:


Then I was thinking about defining a variable of "rval" then using window.location.search to get the numbers (12345). And once I have that input "rval" into the 'defaultValues' field.

Do you think that this is the most efficient way? Or do you have a better way?

(PS. If you can't understand what I'm trying to say then please ask. I'll get back instantly)

Thanks -Chris

05-30-2012, 05:45 PM
Yes, ok, your description is perfectly clear. So what is your problem? If you specify it like this

var rval = window.location.search.substring(1);

(as you just proposed), it should already work. But I don't know what you mean by "defaultValues" field in Javascript. Is it a HTML form field? Is it a Javascript variable?

05-30-2012, 06:08 PM
I'm embedding the survey into a website I created, and there are some values which control the functions of the survey.

var (removed for security);(function(d, t) {
var s = d.createElement(t), options = {
'userName':'(removed for security)',
'formHash':'(removed for security)',

// This is where we add the guest ID number.


I created a script to get the numbers after the ? in the domain:

var querystring = window.location.search;

// Strip the unwanted values
var querystringValue = querystring.substring(querystring.indexOf("=")+1, querystring.length);

// Treat it as a numeric value
var number = querystringValue-0;

And I need to input the variable "querystring" into the defaultValues field. But it keeps coming up as the text "querystring" instead of the variable numbers. I'm writing the code in dreamweaver, and whenever I try to specify the variable it gives me a error.

Hope this clears things up -Chris

Old Pedant
05-30-2012, 11:30 PM
HINT: Never never never trust DreamWeaver (better known as NightmareWaker or maybe DrunkWhacker) when it comes to JavaScript.

Old Pedant
05-31-2012, 12:13 AM
Anyway, you originally said you were going to use something like


But now you have your code looking for an = character in the querystring, as if you had


So now you need some better code to ensure that somebody doesn't use



var qs = location.search;
var rvalue = "";
if ( qs.length > 1 )
var pairs = qs.split("&");
for ( var p = 0; p < pairs.length; ++p )
var pair = pairs[p].split("=");
if ( pair.length == 2 && pair[0].toLowerCase() == "id" ) // or whatever name=value you are looking for
rvalue = decodeURIComponent( pair[1] );
break; // out of the for loop
... now if rvalue is not null, it is ready for use ...