Convert URL parameters to UTF-8 encoding?

whiteboyracer48
whiteboyracer48 used Ask the Experts™
on
Hi,

I need some help to encode a url from Chinese characters to one which is universal.
As an example the URL would be: http://www.mysite.com/folder/CHINESE CHARACTERS HERE 001.JPG

-Edit-
It wont allow me to post the exact url here beacuse the Chinese characters arent recognized but you should get the idea

This script here:
http://javascript.internet.com/navigation/url-decode-encode.html
Is nearly perfect:

when you enter:
http://www.mysite.com/folder/CHINESE CHARACTERS HERE 001.JPG
it returns:
http%3A//www.mysite.com/folder/%E7%85%A7%E7%89%87%20001.JPG

However the code on this script relies on an external .js file and i need it all in one document, e.g. using javascript tags in the header
<script type="text/javascript">
<!--
//-->
</script>

Also this script converts the "http://" to "http%3A//" which is not correct...

So can some kind person please post a full code example of how i convert the external .js file to be in the html header between the javascript tags and add in some code to stop the http at the beginning being converted.

Many thanks in advance.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
You can have the external .js in the same code. Open the .js and copy the code inside the curreent page. Without the .js code I cannot help you.

Commented:
The answer is so much simpler that it looks. There is already a Javascript function that does de job for you, although it converts the ":" to "%3A, but you can convert it back by replacing it inside the original string:
<script language="javascript">
function URLcode(str){
      str=escape(str);
      str=str.replace("%3A",":");
return(str);
}
</script>
for testing purposes:
<script>
alert(URLcode("http://domain.com/!#*=Hello Everybody"));
</script>

And, UTF-8 is a way of coding a Unicode Wide character. URLEncode is a way to encode a byte in a hexadecimal manner. UTF-8 and URL coding are not the same from far.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start Today