Forum Moderators: open

Message Too Old, No Replies

Replacing %20, %40 etc

Getting variables from a url

         

madmatt69

11:19 pm on Feb 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey all,

I'm using this great script that helps me retrieve variables from a URL:

function gup( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );

var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}
var name = gup( 'name' );
var mail = gup( 'from' );

It works well, however in the variables there are spaces and the '@' sign.

I've looked into some tutorials to try and figure this out, but I just can't seem to get it right.

How can I get it to loop through and replace %20 with spaces, and %40 with @?

penders

2:56 pm on Feb 6, 2008 (gmt 0)

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



It sounds as if you need to unescape() the URL first in order to decode the hexidecimal escape sequences....

var results = regex.exec( unescape(window.location.href) );

madmatt69

5:05 pm on Feb 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Wow - I had written like 4 other variables manually changing the characters.

unescape makes it so much easier :) thanks!