<mySearch ⁄>
<mySnippets order="rand" ⁄>
<myContacts ⁄><email ⁄>
<windows live messenger ⁄>
<myCurriculum type="pdf" ⁄>
<myBlog show="last" ⁄>
<myNews show="rand" ⁄>
<myNews type="cat" ⁄>
<myQuote order="random" ⁄>Cobarde não é aquele que foge de uma luta, mas sim o que bate no mais fraco
<myPhoto order="random" ⁄>
<myAdSense ⁄>
<myVisitorsMap ⁄>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Encode Html Special Chars</title> <script type="text/javascript" src="EncodeHtmlSpecialChars.js"></script> <script type="text/javascript" src="EncodeHtmlSpecialCharsInit.js"></script> </head> <body> <div id="txt"></div> <div id="txt_verbose"></div> <textarea id="txtarea" cols="40" rows="5"></textarea> </body> </html>
/** * Encode html special chars * * @author pedrocorreia.net * * @return Function Encode * @return Function Html */ var HtmlSpecialChars = function() { //cache document var _d = document; /** * Get Html Object reference, by its ID * * @param String Id Object * @return Object */ var $ = function(o) { return _d.getElementById(o) || null; }; /** * Create Dom Element * * @param String Element to create * @return Object */ $.createElement = function(o) { return _d.createElement(o) || null; }; /** * Create Element TextNode and add content to it * * @param String Content * @return Object */ $.createTextNode = function(txt) { return _d.createTextNode(txt) || null; }; /** * Add Html Text * * @param String Field * @param String Content to add */ var _AddHtmlText = function(f, str) { $(f).innerHTML += str; }; /** * Encode String * * @param String Verbose output */ var _Encode = function(target, str, _verbose_output) { var elem = $.createElement("div"); elem.innerText = elem.textContent = str; $(target).innerHTML = elem.innerHTML; if (_verbose_output) { //output the html special chars, this will //print "=>" &=>& etc, etc $(_verbose_output).appendChild($.createTextNode(elem.innerHTML)); } elem = null; }; return { Encode: _Encode, Html: _AddHtmlText }; };
window.onload = function() { //our example string var str = "& Simple text with html tags <> <img src=\"\" /><br />"; str += "<span>testing ....</span>"; //create HtmlSpecialChars Object var enc = new HtmlSpecialChars(); //insert text by converting a string to its correspondent HtmlSpecialChars and //output the encoded text to a div called "txt_verbose" enc.Encode("txt", str, "txt_verbose"); //insert the same text but to a textarea enc.Encode("txtarea", str); //clear resources enc = null; //add normal html statement, in this case innerHTML //will treat this as a "normal" <strong> tag HtmlSpecialChars().Html("txt", " ... <strong>This text's in bold</strong>"); };
