So I’ve had a problem of displaying other people’s HTML on my page and their invalid HTML messing up my page’s formatting. So, I found a solution after digging through the Outlook Web Application (OWA) HTML to see how they handle the issue. I do it a little differently, but the idea is the same.
Basically, you have a blank page in an iFrame and you insert the encoded HTML into that blank page. Here’s how I do it:
Setup a blank.htm like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title></title>
</head>
<body>
<div id="htmlcontent"></div>
</body>
</html>
Okay, so now on the page you need to display the HTML, you add this:
<iframe id="htmlcontent" frameborder="0" src="/blank.htm" width="600" height="400" style="background: #fff; margin: 0; padding: 0; border: 0">
<p>Your browser does not support iframes.</p>
</iframe>
Now all we need to do is setup the jQuery like this:
<script language="javascript" type="text/javascript">
function showHtml(id) {
$("#htmlcontent").contents().find("#htmlcontent").html($('#' + id).text());
}
</script>
Now to call the jQuery:
<a href="#" onclick="showHtml('myEncodedHtmlDiv');">Sample Link</a>
<div id="myEncodedHtmlDiv" style="display: none"><p><strong>Greetings from the Encoded Sample!</strong></div>
With ASP.NET, you can do Server.HTMLEncode(htmlToEncode) to encode whatever string you want. The jQuery automatically decodes it when you call the .text() inside the .html() like I did above.
If you want the HTML to show up in a modal, all you need to add is this:
<div id="htmlmsg" style="display: none" class="modal">
<iframe id="htmlcontent" frameborder="0" src="/blank.htm" width="600" height="400" style="background: #fff; margin: 0; padding: 0; border: 0">
<p>Your browser does not support iframes.</p>
</iframe>
</div>
<div id="overlay" style="display: none"></div>
Here’s the CSS to tag along:
#overlay {position: absolute;
left: 0px;
top: 0px;
width:100%;
height:100%;
text-align:center;
z-index: 1000;
background: #eeeeee 50% 50% repeat; opacity: .80;filter:Alpha(Opacity=80);
}
.modal { z-index: 10000;
position: absolute;
top: 100px;
margin: 0 auto;
background: #fff;
border:1px solid #ccc;
padding:10px;
text-align:center
}
Now you’re done. Please comment if you have any questions. Thanks for reading!