问题
I'm loading a page into an IFRAME, and I would like to know if the download was incomplete .. and the HTML is malformed, and missing the closing tags like </body>
and </html>
.
Is there any way to detect this in JavaScript?
回答1:
I'm not sure if you can test for malformed HTML or missing elements without knowing exactly what to expect and testing for it specifically.
If you have access to the source of the iframe page you could call a javascript function variable before the end of the </body>
bound to the parent window; (or use the window.onLoad() event to call trigger the call.)
Define this method in your container page.
window.iAmLoaded = function() {
alert('iframe loaded OK');
};
Then call it from the iframe;
<script type="text/javascript" charset="utf-8">
window.parent.iAmLoaded();
</script>
</body>
If this is not an option, and the iframe has a fairly well known DOM, or a distinctive element near the end of the page, you can use getElementById or your favourite library's CSS selector method to locate the element.
回答2:
You can use the onLoad event for the iframe to tell when its completed loading. http://www.w3schools.com/jsref/jsref_onload.asp
For detecting malformed html: You're looking for a validator. Browsers don't do that, except, maybe, for xhtml strict. There could well be js validation software.
Larry
来源:https://stackoverflow.com/questions/932075/detect-if-iframes-document-is-malformed