Detect if IFRAME's document is malformed

我与影子孤独终老i 提交于 2019-12-11 16:28:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!