JavaScript encoding encode and decode escape and unescape

Source: Internet
Author: User



The encodeURI () function encodes a string as a URI.


Grammar
encodeURI (uristring)
Parameters Description
Uristring Necessary. A string that contains the URI or other text to encode.
return value


A copy of the uristring, where some of the characters will be replaced by a hexadecimal escape sequence.


Description


The method does not encode ASCII letters and numbers, nor does it encode these ASCII punctuation marks:-_. ! ~ * ' ().



The purpose of this method is to fully encode the URI, so the encodeURI () function is not escaped for the following ASCII punctuation mark with a special meaning in the URI:;/?:@&=+$,#


Hints and Notes


Tip: If the URI component contains delimiters, such as? and #, each component should be encoded using the encodeURIComponent () method


<! doctype html>
<html>
<head>
<meta charset = "utf-8" />
<title> </ title>
</ head>
<body>
<script type = "text / javascript">
// Test function for encoding URI
     var url = "http://www.nframes.com.cn/index.html?search=m% ^^ $ # e";
     var res = encodeURI (url);
     document.write (res);
     var res1 = decodeURI (res);
     document.write (‘<br ‘+res1+‘<br/>);
</ script>
</ body>
</ html>






result:

http://www.nframes.com.cn/index.html?search=m%20%25%5E%5E$#e
http://www.nframes.com.cn/index.html?search=m% ^^ $ # e
 

JavaScript escape () function
JavaScript Global Object

Definition and usage
The escape () function encodes a string so that it can be read on all computers.

grammar
escape (string)
Parameter Description
string Required. The string to be escaped or encoded. return value
A copy of the encoded string. Some of these characters are replaced with hexadecimal escape sequences.

Description
This method does not encode ASCII letters and numbers, nor does it encode these ASCII punctuation characters: * @-_ +. /. All other characters are replaced by escape sequences.

Tips and notes
Tip: You can use unescape () to decode escape () encoded strings.

Note: This method is deprecated by ECMAScript v3 and applications should use decodeURI () and decodeURIComponent () instead.

Examples
In this example, we will use escape () to encode the string:


<! doctype html>
<html>
<head>
<meta charset = "utf-8" />
<title> </ title>
</ head>
<body>
<script type = "text / javascript">
// Test function for encoding URI
     var url = "http://www.nframes.com.cn/index.html?search=m% ^^ $ # e";
     var res = escape (url);
     document.write (res);
     var res1 = unescape (res);
     document.write (‘<br ‘+res1+‘<br/>);
</ script>
</ body>
</ html>













Output:

http% 3A // www.nframes.com.cn / index.html% 3Fsearch% 3Dm% 20% 25% 5E% 5E% 24% 23e
http://www.nframes.com.cn/index.html?search=m% ^^ $ # e

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.