Click to See Complete Forum and Search --> : [RESOLVED] urlencode in JavaScript?


mikesch
02-24-2006, 02:09 PM
Hi,

I have a php page where I output a list of titles and give the option to delete them. When the user clicks on delete, I call the following JavaScript function:

function deleteitem(title) {
if (confirm("Are you sure you want to delete '" + title +"'"))
{
window.location.href = 'outputadmin.php?del=' + title;
}
}

If the user confirms, then the title is deleted from the database. Everything works as long as I don't have a title that contains the '&' (ampersand) character. For example, if I have the following title: 'John & Mary', then after the user confirms his wish to delete, the following url will be sent: outputadmin.php?del=John&Mary

The problem is that when I access $_GET[del] I will get $_GET=John and that's not the title in the database. I know 'Mary' is treated as separate variable just like 'del'.

Is there a way around this in JavaScript to urlencode the whole title? In other words, to skip the '&'?

Thank you very much.

Best,

Mike

mikesch
02-24-2006, 03:12 PM
Found the answer. FYI: In Javascript, the function escape() does the same as urlencode() in PHP.

Ichiro
03-20-2007, 10:05 AM
Actually Javascript's escape() and unescape() are not the same as PHP's urlencode() and urldecode(). In fact, there is one particular occasion when assuming they are the same can cause serious trouble.

You can see a table of the results of the two different methods at http://cass-hacks.com/articles/discussion/js_url_encode_decode/

I know this was posted a long time ago but I just got here. :D

stijnm
10-23-2007, 06:29 AM
Just got here too.

How about this:

function urlencode(str) {
str = escape(str);
str = str.replace('+', '%2B');
str = str.replace('%20', '+');
str = str.replace('*', '%2A');
str = str.replace('/', '%2F');
str = str.replace('@', '%40');
return str;
}

function urldecode(str) {
str = str.replace('+', ' ');
str = unescape(str);
return str;
}

nubs
09-29-2008, 07:58 PM
Just got here too.

How about this:

function urlencode(str) {
str = escape(str);
str = str.replace('+', '%2B');
str = str.replace('%20', '+');
str = str.replace('*', '%2A');
str = str.replace('/', '%2F');
str = str.replace('@', '%40');
return str;
}

function urldecode(str) {
str = str.replace('+', ' ');
str = unescape(str);
return str;
}

This is shorter:

function urlencode(str) {
return escape(str).replace('+', '%2B').replace('%20', '+').replace('*', '%2A').replace('/', '%2F').replace('@', '%40');
}

function urldecode(str) {
return unescape(str.replace('+', ' '));
}

ottodv
11-02-2008, 04:00 PM
The solutions above only replaces the first occurrence of each pattern. It's better to use a global replace:

function urlencode(str) {
return escape(str).replace(/\+/g,'%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
}

ottodv
11-07-2008, 10:16 PM
In addition to my previous post I think it would be good to point out that there is also the encodeURIComponent() (https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference:Global_Functions:encodeURIComponent) function in JavaScript that is probably suitable for the purposes intended in this thread. It doesn't escape: ! ~ * ' ( ) like the urlencode PHP function does, but I have experienced no problems passing a argument encoded by encodeURIComponent to a PHP script. Another plus is that encodeURIComponent encodes all UTF-8 characters while escape encodes ISO Latin characters.

ProteusR3
06-25-2009, 04:33 AM
I've got another solution.
I've tested it and it works very well!
function php_urlencode (str) {
str = escape(str);
return str.replace(/[*+\/@]|%20/g,
function (s) {
switch (s) {
case "*": s = "%2A"; break;
case "+": s = "%2B"; break;
case "/": s = "%2F"; break;
case "@": s = "%40"; break;
case "%20": s = "+"; break;
}
return s;
}
);
}