简体   繁体   中英

iframes using directly html code, no source

I would like to embed HTML code into a html page. The styles from the HTML embeded code is disturbing the complete layout, that´s why I want to use iframes. The problem is that there is no .html page to link to, because this HTML code is comming from the database. I have tryed something like:

<iframe>${htmlContent} </iframe>

Where ${htmlContent} contains a simple html content.

<html>
<head>
<title></title>
</head>
<body>
</body>
</html>

This answer assumes you're able to strip out the <html> and </html> tags from your stored HTML, and are able to HtmlEncode it when placing into the your markup.

Store the encoded HTML into something like a div...

<div id="htmlstore" style="display:none;">{html goes here}</div>

So it looks something like...

<div id="htmlstore" style="display:none;">&lt;body&gt;This is my text&lt;/body&gt;</div>

Then use the following Javsacript, which will "HtmlDecode" (there is no native javascript function to do this unfortunately) and then place into the iframe

<script type="text/javascript">
  window.onload = function(){
    var html = document.getElementById("htmlstore").innerHTML;
    html = html.replace(/&lt;/g,"<").replace(/&gt;/g,">");
    var frameDoc = document.getElementById("newframe").contentWindow.document;
    frameDoc.documentElement.innerHTML = html;
  }
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM