demo
templates
install
contact
Home Tips Encoder

Code Encoder Script

To display the raw HTML code on a web page, you must first convert all special characters by encoding them. The script on this page does that for you. Once encoded, the resulting code can be pasted into this template system so it will display properly as code in a web page. In effect, it prevents the code from being executed by the browser. The opening square bracket '[' for BBCode is also encoded as [ to prevent them from being interpreted by Contentor.


The Encoder Form:



The JavaScript Code:


<script>
function format(ans) {
    var strhld = ans.elements[1].value.replace(/&/g,'&amp;');
    strhld = strhld.replace(/  |\t/g,'&nbsp; ');
    strhld = strhld.replace(/</g,'&lt;');
    strhld = strhld.replace(/\[/g,'&#091;');
    // strhld = strhld.replace(/\r\n/g,'<br />\r\n');
    ans.elements[1].value  = strhld;
}
</script>

The Form:

<form action="" method="post" name="testform" id="testform">
  <input name="fmat" type="button" value="Format Code" onclick="format(this.form);" />
<br />
<textarea name="text" cols="70" rows="15">Enter some code here & click the "Format Code" button above *once*.</textarea>
</form>



The last replacement line is commented out since it adds in '<br />' tags for proper display of the code. These tags are added automatically by Contentor, so are not needed in this verion of the script.

The 'elements' number (in this example = 1) is set to element number of the form. In this example, the "Format Code" button is element '0' and the textarea is element '1'. An optional submit button could be added to send the code to a forms processing script.