Forum Moderators: open

Message Too Old, No Replies

Dynamically defining Undefined variables

I want to replace Undefined variables coming from a form

         

dolphinsdock

7:38 pm on Jan 3, 2006 (gmt 0)

10+ Year Member



I'm trying to pass variables from a form from one page to the next. On the recieving page, I am using the following script to convert the form field values into js variables.


function getParams() {
var idx = document.URL.indexOf('?');
var params = new Array();
if (idx!= -1) {
var pairs = document.URL.substring(idx+1, document.URL.length).split('&');
for (var i=0; i<pairs.length; i++) {
nameVal = pairs[i].split('=');
params[nameVal[0]] = nameVal[1];
}
}
return params;
}
params = getParams();

Then I'm using this in the body to display the field values in the appropriate places.


C1 = unescape(params["C1"]); document.write(C1);

The problem I'm having it that empty fields display as "undefined" I'd like for empty fields to equal a string of " ", obviously while not disturbing the fields that already contain a value.

I attempted to add


if (typeof params == "undefined") {params = ' ';}

to the getParams function but it did change anything.

I'm fairly new to javascript so your help is very much appreciated.

Fotiman

7:54 pm on Jan 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




if (typeof params == "undefined") {params = ' ';}

It's not params that's undefined... it's params["C1"].



if( typeof params["C1"] == "undefined" )
{
C1 = "";
}
else
{
C1 = unescape(params["C1"]);
}

document.write(C1);


dolphinsdock

8:15 pm on Jan 3, 2006 (gmt 0)

10+ Year Member



Makes sense, and worked great. Thanks.