[ZT] Customer Service htmldecode and htmlencode-in Javascript

Source: Internet
Author: User

Today, I encountered a problem. To implement paging through Ajax, we need to return the htmlCodeEnd re-assembly, one is paging, and the other is the query result. In this case, how can we separate the code at both ends? The method I came up with was to construct a JavaScript Object in the ajaxmethod and add the two pieces of code to the object's property value.

" // Construct JavaScript Object
VaR Objhtml =   New Object ();
Objhtml. firstsection =   ?;
Objhtml. secondsection =   ?;
"

Then, construct the returned JavaScript Object through eval (strcode) in the front-end callback function. Then, it is very easy to update the corresponding HTML block.
I can replace the question mark in the above Code with two HTML code block strings, but I want to decode the front-end if I add encode to them, I tried to add the server in the background. urlencode (strhtml), the foreground needs to set uridecode () and unecape (), but the space inside is still '+', adding a Replace (/\ +/g ,'') you can solve it. The potential problem is that if there is '+' in the content, there will be a problem. So I searched the internet to see if there is any better solution, the result shows that a clever method is implemented using containers in HTML.
Javascript htmlencode implementation : Function Htmlencode (strhtml)
{
VaRDiv=Document. createelement ('Div');
Div. innertext=Strhtml;
ReturnDiv. innerhtml;
}

Javascript htmldecode implementation : Function Htmldecode (strencodehtml)
{
VaRDiv=Document. createelement ('Div');
Div. innerhtml=Strencodehtml;
ReturnDiv. innertext;
}

This implementation is convenient and simple. The following is the sample code for your reference. < Script Type = " Text/JavaScript " >
Function Htmlencode (strhtml)
{
VaRDiv=Document. createelement ('Div');
Div. innertext=Strhtml;
ReturnDiv. innerhtml;
}
Function Htmldecode (strencodehtml)
{
VaRDiv=Document. createelement ('Div');
Div. innerhtml=Strencodehtml;
ReturnDiv. innertext;
}
Function Test1 ()
{
VaR Strinput = Prompt ( ' Test htmlencode (), Please input decodehtml ' , '

5
' );
Alert (htmlencode (strinput ));
}
Function Test2 ()
{
VaR Strinput = Prompt ( ' Test htmldecode (), Please input encodehtml ' , ' & lt; Div style = "width: 600px;" & gt; & lt; input type = "button" name = "runstart" value = "View" onclick = "Run ()" style = "margin-left: 2px;" & gt; & lt; input type = "button" name = "runstart" value = "runscript" onclick = "runscript ()" style = "margin-left: 10px;" & gt; & lt; input type = "button" name = "reset" value = "clear" onclick = "RESET ()" style = "margin-left: 18px;" & gt; & lt; SPAN onclick = "expandcollapse (this);" onmouseover = "showmsg ();" onmouseout = "hidemsg ();" MSG = "Expand" style = "font-family: webdings; font-size: 18pt; cursor: pointer; "& gt; 5 & lt;/span & gt; & lt;/Div & gt; & lt; IFRAME name = "ifrshow" style = "margin: 0px; margin: 0px; width: 600px; Height: 400px; Border: 1px orange solid;" & gt; & lt; /iframe & gt; & lt; Div id = "divmsg" style = "display: none" & gt; & lt;/Div & gt; ' );
Alert (htmldecode (strinput ));
}
< / SCRIPT> <button onclick = "test1 ();"> test htmlencode < / Button >
< Button onclick = " Test2 (); " > Test htmldecode < / Button>

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.