Loading Iframes with JavaScript

Of course, you don’t have to write into the contents of an iframe with JavaScript; chances are you’ll want to load other HTML pages. This example shows you how. Once again, we’ll have a main page that sets up the iframe and will be virtually identical to Script 5.14. Similarly, there is a page with the initial content of the iframe, like Script 5.15. There are also three other simple HTML pages (not shown) that we load into the iframe with Script 5.17.

To load an iframe with JavaScript:

  • function setiFrame() {
      document.getElementById("content").contentWindow.document.location.href = this.href;
      return false;
    }

    In much the same way that Script 5.12 set both the target and the onclick handler for the links, Script 5.17 ...

Get JavaScript and Ajax for the Web: Visual QuickStart Guide, Seventh Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.