AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Html iframe with java script code4/22/2024 ![]() ![]() This simple message is just a proof-of-concept. $(elem).attr('onerror',"'srcdoc','You need to be online to view this content.') ").attr('onload', "if(navigator.onLine=false) eval(this.getAttribute('onerror')) ") }) }) Ĭaution: Using this global technique, you may also want to check to make sure you’re only altering iframes where the src attribute is online content ( e.g., contains ://). Here’s how you would do the same exactly thing, but with a single code block ( i.e., not hand-altering every iframe in your document). This could also be done with in5 content, by attaching the JavaScript as an external resource. Handling all iframes with a single block of code I purposely wrote the code so that everything could be included with the iframe itself, because many of our in5 customers are pasting the iframe embed code into their InDesign documents for digital publication.Īlternatively, you could write a block of code that handles an entire document full of iframes. This jsfiddle has the source code for 4 different examples. I used the strict equivalence operater (=), as opposed to the equivalence operator (=), to ensure that this would not be triggered if navigator.onLine was undefined, which would be the case in an older browser. Since I’m lazy and I don’t like to re-write (and re-writing introduces opportunities for mistakes), if the browser is offline, this will execute the code that was already written for the onerror attribute. Onload="if(navigator.onLine=false) eval(this.getAttribute('onerror')) " So I added the following onload attribute to the iframe tag. Newer browsers (including Chrome and Safari) support the navigator.onLine property. Using the jQuery replaceWith method to replace the iframe tag.ĭepending on the URL I used, I either got a silent error with an empty iframe, or content generated by the browser.Setting the srcdoc attribute, which lets you apply HTML code as the iframe content.I’m leaving the article in place in case it helps with understanding the issue, but this is probably a better solution.Īdding an onerror attribute looked like a promising solution…at first…because I was only testing in Firefox. ![]() Note: The onerror event no longer seems to fire when used with an iframe. ![]()
0 Comments
Read More
Leave a Reply. |