Search code examples
htmljspiframealt

Alternate text for iframes


We have alternate text, alt attribute, for an img tag in HTML, which will show up when the image doesn't come up. I tried using the tag with iframe too.

<iframe src="www.abc.com" alt="Web site is not avaialable">

But the alternate text doesn't come up when src="" is given. Just wanted to know if I can achieve alternate text in any other way, if the src is not given ?


Solution

  • Since my first attempt misunderstood your question, let's try this instead:

    <script>
    $(function () {
    
        $("iframe").not(":has([src])").each(function () {
    
        var ifrm = this;
    
        ifrm = (ifrm.contentWindow) ? ifrm.contentWindow : (ifrm.contentDocument.document) ? ifrm.contentDocument.document : ifrm.contentDocument;
    
        ifrm.document.open();
        ifrm.document.write($(this).attr("alt"));
        ifrm.document.close();
    
        });
    
    });
    </script>
    

    This will read the "alt" tag value for any iframe with either no src attribute or a src attribute with a blank value, and write the alt text into the body of that iframe.

    Assist from Write elements into a child iframe using Javascript or jQuery