简体   繁体   中英

HTML Renders in IE7 but not in Firefox or Chrome

The following html in an <iframe> renders in IE7 but not in Firefox or Chrome?

var content = "<!DOCTYPE html PUBLIC \"-//WAPFORUM//DTD XHTML Mobile 1.2//EN\"\"http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd\">
<html>
 <body style=\"background-color:#0C0C0C; color:#FFFFFF\"> 
 Please Enter the credentials
 <form name=\"dynamicform\">
 <ul class=\"edgetoedge\" style=\"background-color:#0C0C0C;\"><li><div id=\"errorDiv\" style=\"color:red\"> </div></li> <li> <input id=\"Phone Number:_minLength\" type=\"hidden\" value=\"16\" /> </li>
 <li> </ul> </form> </body> </html>"
<script>
.....
var dynamicFormIframe = document.getElementById('dynamicFormIframe');
dynamicFormIframe = (dynamicFormIframe.contentWindow) ? dynamicFormIframe.contentWindow : (dynamicFormIframe.contentDocument.document) ? dynamicFormIframe.contentDocument.document : dynamicFormIframe.contentDocument;
        dynamicFormIframe.document.open();
        dynamicFormIframe.document.write(content);
....</sript>
<body><iframe id="dynamicFormIframe" src=""></frame></body >
dynamicFormIframe = (dynamicFormIframe.contentWindow) ? dynamicFormIframe.contentWindow : (dynamicFormIframe.contentDocument.document) ? dynamicFormIframe.contentDocument.document : dynamicFormIframe.contentDocument;

contentDocument.document is nonsense; that clause will never be taken. Chrome, not supporting the non-standard contentWindow property, will fall back to using contentDocument , which is a different object from contentWindow .

You only seem to want the document, not the window, so go for the standard contentDocument first, and only fall back to going via the window for IE where it's not supported:

var iframe= document.getElementById('dynamicFormIframe');
var idoc= 'contentDocument' in iframe? iframe.contentDocument : iframe.contentWindow.document;
idoc.open();
idoc.write(content);
idoc.close();

(Your example also has many apparent typos like mismatching tags, a JS string split over lines and a malformed doctype, is that a copy-and-paste error?)

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