views:

269

answers:

2

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?

A: 

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

Larry K
Strict / Transitional is irrelevant. Browsers will choke with parsing any non-well-formed XHTML in XML mode (i.e. when the document is served with the correct content type). Most browsers that support XHTML will then offer to try parsing again in tag soup mode. A document can be well-formed without being valid though.
David Dorward
+2  A: 

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.

garrow