View Full Version : Unescape Not Working

02-02-2004, 01:55 AM

I've put up a form on my website that is supposed to pass the entered values from one page to a new page that opens and show the data exactly as typed on the new page. I'm using a javascript that I got from another javascript site that i have modified to fit my needs. For some reason, any spaces that are input into the form are still being displayed on the next page as "+" signs. For example, if I input "New York," it will show up on the new page as "New+York." How do I fix this problem? There is already coding in place that uses the unescape function, but it still won't display properly. Could someone please have a look at my webpage to see if they can figure out the problem? It's at melaniestype.com/serviceagreement.html. The output form is at melaniestype.com/serviceagreementoutput.html. I'd appreciate a quick response. Thanks.

02-02-2004, 02:28 AM
<!-- Begin
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();
// End -->

<!-- STEP THREE: Put this on the page that should read the values -->

<BODY link=forestgreen vlink=forestgreen><nobanner>


<!-- Begin
requiredclientname = unescape(params["requiredclientname"]);
requiredclientID = unescape(params["requiredclientID"]);

When I say

document.write("Client name is " + requiredclientname + ".");

for example, it prints wrong.

Willy Duitt
02-02-2004, 03:40 AM
Here's a quick fix:

requiredclientname = unescape(params["requiredclientname"]).replace(/\+/g, " ");


02-02-2004, 03:45 AM
Oh, thank you so much! Finally works.

But how does that code actually work? What's with the front and back slashes? Sorry if I sound like, duh, cause I'm new to this whole thing.

02-02-2004, 03:47 AM
You should unescape the name also:

params[unescape(nameVal[0].replace(/\+/g, " "))]=unescape(nameVal[1].replace(/\+/g, " "));

See this thread (http://www.codingforums.com/showthread.php?s=&threadid=25786) for the issues in parsing querystrings.

Willy Duitt
02-02-2004, 04:07 AM
It's a regular expression and since I am still struggling with them,
I am not qualified to offer a substantive explanation. But, I'll try. :D

The forward slashes / are the wrapper.
The backslash \ escapes the reserved +
And the g indicates it is to be global. (replace all instances of)

Liorean has a good tutorial on regular expressions
but I do not have the link handy. :o

Furthermore, Glenns solution is much better ;)
Not only will the pairs name be covered, you won't
need to add the .replace(...) to all of your values.


02-02-2004, 04:20 AM
Thanks a lot, you guys. I had already used your idea, Willy, before Glenn had a chance to post, and I don't really understand how your code works, Glenn. I follow it kind of, but I don't understand the namVal or the brackets with [0] and [1] following namVal. Would "namVal" go in place of the names I have, i.e. "requiredclientname"...? Could you explain that code to me, Glenn, in basic terms?

02-02-2004, 04:35 AM
Maybe this is clearer...

var name, value, nameVal;
for (var i=0; i<pairs.length; i++) {
nameVal = pairs[i].split('='); //returns an array with 2 items
name = unescape(nameVal[0].replace(/\+/g, " ")); //unescape item 1 from the array and replace +'s with spaces
value = unescape(nameVal[1].replace(/\+/g, " ")); //unescape item 2 from the array and replace +'s with spaces
params[name] = value; //put the name-value in "hash array"

and then you access the names like these:

var requiredclientname = params["requiredclientname"];
var requiredclientID = params["requiredclientID"];

Read the links I posted for more comprehensive discussion on this topic.

02-02-2004, 04:59 AM
Thanks, Glenn

That IS a lot clearer. Boy, this site is a very big help. I had been trying my best to fix the problem myself to no avail. I'm glad you guys were there to help. Thanks again. Take care :thumbsup: