troubleshooting Question

Remove style from DOM when placed in iframe

Avatar of hankknight
hankknightFlag for Canada asked on
7 Comments2 Solutions326 ViewsLast Modified:
I use the following code to dynamically place content in an iframe.  

The problem is that if there are bad syles in the code, the bad styles stay even after the content is placed in iframe.

The problem only seems to be in Firefox.

If you test my code in Firefox you will see the problem.

Thanks for the help!
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<h1>Dynamically Place Content of div in Iframe</h1>
<div id="postentry">
<style type="text/css">
html, body { background: red;}
<body><h1>>Bad Code2<h1>tstt<h3>Only the iframe should be red<h3>
<script type="text/javascript">
	var messageContent = document.getElementById('postentry');
	if (messageContent) 
		var htmlContent = messageContent.innerHTML;
		messageContent.innerHTML = '';
		var iFrame = document.createElement("iframe");
		iFrame.marginWidth = "0";
		iFrame.frameBorder = "1";
		iFrame.height = "500px";
		iFrame.width = "100%"; = "999";
		var htmlPost = document.getElementById('postentry');
		if (htmlPost) 
			var displayFrame = this.document.getElementById("ifhsf2gxvzw54vx");
			var doc = displayFrame.contentDocument;
			if (doc == undefined || doc == null)
			    doc = displayFrame.contentWindow.document;
			doc.body.leftMargin = "0px";
			doc.body.rightMargin = "0px";
			doc.body.topMargin = "0px";
			doc.body.bottomMargin = "0px";

Open in new window

Join our community to see this answer!
Unlock 2 Answers and 7 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros