Hack for a Problem with IE Displaying HTML Content via the OBJECT Tag

If you find this code useful and would like to encourage me to post more goodies here, you could buy me something from my wish lists at Amazon.com or at MusicDirect.com

I just recently discovered that IE7, when running with default security settings for the "Internet zone", will not display HTML content from another site via the <object> tag, while providing no obvious indication to the user as to why. The simple work-around is to use an <iframe> element, instead, but this has the problem of being a deprecated tag which will not validate if a "strict" doctype is used.

The work-around I came up with is to use IE conditional comments, displaying an <iframe> if in IE, otherwise displaying an <object>. This ensures that IE users with higher security settings will still see the desired off-site content, while keeping the document strictly valid (as the content within the IE conditional comment is ignored by other browsers).

Here's a sample page using this technique, and below is the source code of that page. The important part is the section starting with "<!--[if IE]>".


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang='en'>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=ISO-8859-1'>
<title>test</title>
<style type="text/css">
#yahoo {
   width: 100%;
   height: 300px;
   margin: 0 auto;
   overflow: auto;
}
</style>
</head>
<body>
<div>
<!--[if IE]>
<iframe src="http://www.yahoo.com/" id="yahoo">
Your browser does not support frames, but you may view this content
at <a href="http://www.yahoo.com/">www.yahoo.com</a>
</iframe>
<![endif]-->
<!--[if !IE]> <!-->
<object data="http://www.yahoo.com/" type="text/html" id="yahoo">
Your browser does not support objects, but you may view this content
at <a href="http://www.yahoo.com/">www.yahoo.com</a>
</object>
<!--<![endif]-->
</div>
</body>
</html>