
In order for you to display this as is, you may want to clean your codes first into an browser friendly text by converting some characters to its HTML code equivalent.
The Online HTML Converter
The Logic Behind
What we are trying to do is convert all<
and >
signs into its HTML equivalent. Reason is, browsers treat this as tags and everything inside of <
as parameters or attributes of the tag.
We will convert &
signs too for other languages such as JavaScript codes.
HTML
This code are just plaintextarea
s (one for the raw and one for the processed) and two buttons for converting and reverting the process
<textarea class="codeInput" id="codeInput" cols="50" rows="10"></textarea> <br/> <textarea class="codeOutput" id="codeOutput" cols="50" rows="10" /></textarea> <br/> <input type="button" id="codeConvert" value="Convert" /> <input type="button" id="codeRevert" value="Revert" />
JQuery
I'll be using JQuery in here instead of plain JavaScript. Logic might be the same though.$("#codeConvert").click(function () { $codeInput = $("#codeInput").val(); $codeOutput = $codeInput.replace(/\&/g, "&").replace(/\>/g, ">").replace(/\</g, "<"); $("#codeOutput").val($codeOutput); }); $("#codeRevert").click(function () { $codeOutput = $("#codeOutput").val(); $codeInput = $codeOutput.replace(/\>/g, ">").replace(/\</g, "<").replace(/\&/g, "&"); $("#codeInput").val($codeInput); });
nice sharing..
ReplyDeleteTech Tips & Tricks