HTML encoding and URL encoding __ encoding

Source: Internet
Author: User

Finally understand the two coding of the use of the scene, to share under. one, HTML encoding

When a label in an HTML page wants to display <a>test</a>, if you simply write label.html ("<a>test</a>"), the page displays a label with the content test.

This is because <, > is a special character in HTML, parsing will be considered HTML tags, at this time to display a page on a, you need to HTML encoding, < escape for &lt; on OK ~

Provides a method for HTML encoding and decoding of JS

function HTMLEncode (s) {  
    var div = document.createelement (' div ');  
    Div.appendchild (document.createTextNode (s));  
    return div.innerhtml;  
}  
function HtmlDecode (s) {  
    var div = document.createelement (' div ');  
    div.innerhtml = s;  
    return div.innertext;  
}

In addition, when using MVC, @Model. Name is already HTML coded for this field, so this is generally not a problem.

However, note that if you want to display in the text Vlaue, in fact, do not need to Html encoding, at this time need to use @Html. Raw(model.name) This form to get the original content. Second, URL coding

URL encoding is a format that browsers use to package form input. The browser takes all the name and the values from the form, encodes them as a name/value parameter (removing those that cannot be transferred, ranking the data, and so on) as part of the URL or sending it to the server in isolation.

Any special characters (that is, those that are not simple seven-bit ASCII, such as Chinese characters) will be encoded in percent hex, including, of course, like =,&;, and% these special characters. In fact, the URL encoding is a character ASCII hex. However, there are some changes that need to be preceded by the words "%".

Above from Baidu Encyclopedia


Give a use of the scene, such as the page jump parameters,? a= Chinese +, if directly redirected to this link, + special characters will be filtered out, this time need to do URL coding

JS with the URL encoding method, encodeURI (), encodeURIComponent ()

Corresponding URL decoding method, decodeURI (), decodeURIComponent ()


The method of URL encoding in btw:c#
Encoding: Server.URLEncode (String)
Decoding: Server.urldecode (String)



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.