
In an application, the user could create emails and save them as templates. To illustrate how this isolation from the JavaScript and CSS is handy, let’s take a look at these two situations: The iframe can still behave in annoying or malicious ways: triggering a popup or auto-playing videos for instance. Nonetheless, as you will see in this guide, the separation is not so perfect. That is one of the valid purposes to use an iframe: to provide a measure of separation between your application and the iframe content. Thus, it will be isolated from the JavaScript and CSS of the parent. What you must keep in mind when thinking about an iframe is that it lets you embed an independent HTML document with its browsing context. Here is another example in which we display a button to tweet your web page on Twitter:
#IFRAME ONLOAD CODE#
You may have crossed paths with it when you had to include a third-party widget (like the famous Facebook like button), a YouTube video, or an advertising section on your website.įor instance, the code below will display a 500px square with the google homepage within: What is an iframe, and when do you use it?ĭevelopers mainly use the iframe tag to embed another HTML document within the current one. Finally, we’ll talk about how you can secure your iframe to avoid potential vulnerabilities. We’ll go through most of the features the iframe element provides and talk about how you use them, as well as how iframe can be useful for overcoming some tricky situations. To help you form your own opinion and sharpen your developer skills, we will cover all the essentials you should know about this controversial tag.


They have many legitimate uses cases.īesides, it’s not that hard to secure them, so you won’t have to worry about your user’s computer becoming infected. I believe that their bad reputation should not prevent you from relying on them. The iframe element (short for inline frame) is probably among the oldest HTML tags and was introduced in 1997 with HTML 4.01 by Microsoft Internet Explorer.Įven though all modern browsers support them, many developers write endless articles advising against using them. Nada also dabbles in digital marketing, dance, and Chinese. She specializes in Vue.js and loves sharing anything and everything that could help her fellow frontend web developers. If it's not, simply attach the event trigger line to the window's load event like so: $(window).Nada Rifki Follow Nada is a JavaScript developer who likes to play with UI components to create interfaces with great UX. Please note, that we're leveraging the DOM ready event to fire the event - which should be suitable for most use cases. More generally speaking, it shouldn't be the concern of the iframe to know its surrounding environment. This solution has the advantage of not breaking when the containing page does not contain the expected load handler.

Then, in the parent window you attach a handler to react to that event. The iframe fires an event on the (potentially existing) parent window's document - please beware that the parent document needs a jQuery instance of itself for this to work. $(document).on('iframeready', myHandler) W.parent.jQuery(w.parent.document).trigger('iframeready') Along the lines of Tim Down's answer but leveraging jQuery (mentioned by the OP) and loosely coupling the containing page and the iframe, you could do the following:
