Preload iFrame using JavaScript

To preload iFrame in JavaScript is possible. Sometimes it takes a while for a iFrame page to be loaded, this causes ‘a moment of blankness’ which is not what we want the user to see. To fix this bad user experience, we could make a preloader to preload the iFrame, only when the iFrame is loaded, then we display the content.

To do so, we could use the existing JavaScript library – jQuery. The script below is an example:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <iframe src ="http://www.yahoo.com" width="100%" height="300"></iframe>
  <img src="http://www.ezeego1.co.in/hotels/india/athirapally/page/webroot/images/preloader.gif" id="preload-img" />
  <script>
  $('iframe').css('display', 'none');
  $('iframe').load(function()
  {
    $('iframe').css('display', 'block');
    $('#preload-img').css('display', 'none');
  });
  </script>
</body>
</html>

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Technorati
  • Twitter
  • Yahoo! Bookmarks

About Shi Chuan

I am a web developer.
This entry was posted in JavaScript Library and tagged , . Bookmark the permalink.

7 Responses to Preload iFrame using JavaScript

  1. Sorry ! But it is not working at all..

  2. Ahmed Kamal says:

    woooooooooooooooow so much thx

  3. WCampo says:

    Fantastic, thank!

  4. wow, thanks but i have a prob. after using this cufon.js stoped working …

    plz. suggest me any solutions.

  5. björn says:

    hi!
    where do have to add this code in order to work ?
    thx & br,
    bjoern

  6. Chritchan says:

    Hey ! Helpfull ! thx for this script ;)

  7. Anup says:

    Very helpful! thank you very much.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">