jquery - HTML input affecting entire page -


i making page can enter html , see output in div side.
code:

$("#tryit").keyup(function() {    var input = $(this).val();    $("#readout").html(input);  });
#readout {    height: 400px;    width: 48%;    border: 1px solid black;    overflow: auto;    float: right;    clear: none;    display: inline-block;  }  #tryit {    height: 396px;    width: 50%;    float: left;    clear: none;    display: inline-block;    font-family: monospace;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <textarea name="tryit" id="tryit" rows="10">&lt;!doctype html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;new webpage&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;/body&gt; &lt;/html&gt;  </textarea>    <div id="readout"></div>

it works should: outputs html #readout. goes wrong when user adds <style> tag in head. style added there affects not html in text input, in entire page.
how can make css affect user's input, , not entire page?
here problem page.

you should consider using different approach achieve this.

if want go have far have replace lot of stuff within textarea value. give idea, in below snippet replacing html , body style declarations id (#readout) apply styles #readout div , not body of page. work body, other possible declarations?

sure real noob when comes regex , sure can done lot better, of not necessary.

$("#tryit").keyup(function() {      var style = $(this).val().match(/<style>(.*)<\/style>/);      if (style !== null && style.length > 0) {          var bodyreplace = style[1].replace(/(^|\s)(html)|(body)(\s|$)/ig, '#readout');      }    var input = $(this).val();    $("#readout").html(input.replace(/<style>.*<\/style>/, '<style>'+bodyreplace+'</style>'));  });    $("#tryit").keyup();
#readout {    height: 400px;    width: 48%;    border: 1px solid black;    overflow: auto;    float: right;    clear: none;    display: inline-block;  }  #tryit {    height: 396px;    width: 50%;    float: left;    clear: none;    display: inline-block;    font-family: monospace;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <textarea name="tryit" id="tryit" rows="10">&lt;!doctype html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;new webpage&lt;/title&gt;<style>body {background: black; color: red }</style> &lt;/head&gt; &lt;body&gt;<h1>hello</h1> &lt;/body&gt; &lt;/html&gt;  </textarea>  <div id="readout"></div>

an other problem <doctype>, <html>, <head> , <body> elements in textarea not exist. take @ source , find out not there , not belong there. being said go solution mentioned in comment barmar, suggested using iframe.

it not hard achieve. 1 quick , dirty way crate blob textarea value , add source attribute of iframe. make easy separate html , css having textarea css , injecting it's value style-sheet head of iframe. since can't use iframe thing in snippet tool because of sandbox security stuff check this

jsfiddle

and sure idea.


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -