javascript - how to post HTML code without the browser rendering -


i'm build forum project, right i'm facing problem want users able post html source code works in forum.

but problem code runs or scatters design when retrieve db.

i tried using repalce() in jquery replace < &lt; want function able replace others such >,",' , & question how can update function.

function convert(div){   var str =  $(div).html();   var str2 =  str.replace(/</g,"&lt;");   var sta =  $(div).html(str2);   return sta; } 

the above code work replace < when try including >,",' , & in function stop work how can make work.

thanks in advance.

stick in <pre> or <code> tags, or both, , make sure use text() when inserting content tag

function convert(div){      var str =  $(div).html();      var sta =  $('<code />', {text : str});        return sta;  }    var result = convert( $('#test') );    $('#result').html(result)
#result {    white-space : pre;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div id="test">    <span>      <p>test</p>    </span>  </div>  <br />  <div id="result">

<code> preserve code, , <pre> preserve whitespace, there's css white-space property, can act <pre> tag using pre setting


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -