Today we will learn how to simply load a website into an iFrame using jQuery. Some of you might have already tried this, but this article is for thoes who have never tried it.
Background
I have been working with an application in which we have a widget called URL widget, we are doing so many things in that widget. Here we will see how to simply load a website into the IFrame by giving the URL of the website to the src property of the iFrame.
Using the code
To start with, as always we need to load the jQuery first.
- <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
The next part is to create some UI elements as in the following:
- <body>
- <p id="loadMe">loadMe</p>
- <b>Load my website here.</b>
- <iframe id="load" src="" ></iframe>
- </body>
Once this is done we can style those elements by giving some styles as follows.
- <style>
- #load {
- position: absolute;
- background-color: blue;
- color: #fff;
- padding: 10px;
- border: 1px solid #ccc;
- border-radius: 5px;
- width: 100%;
- height:100%;
- display:none;
- }
- #loadMe {
- background-color: blue;
- color: #fff;
- padding: 10px;
- border: 1px solid #ccc;
- border-radius: 5px;
- width: 80px;
- height:30px;
- cursor:pointer;
- }
- </style>
Now we will see the jQuery part.
- <script>
- $(document).ready(function () {
- $('#loadMe').click(function (e) {
- $('#load').show();
- $("#load").attr("src", "http://www.sibeeshpassion.com/");
- });
- });
- </script>
As in the preceding code snippet, we are firing a click event in the document.ready function and in the click event we are setting the src attribute of the iFrame.
- $("#load").attr("src", "http://www.sibeeshpassion.com/");
The beauty of iFrame is that whenever we set the src, it will load that website content to that. So shall we see the output now?
Output

Figure 1: Output

Figure 2: Displaying the Result
Complete Code
- <!DOCTYPE html>
- <html>
- <head>
- <title>Load Website to iFrame - Sibeesh Passion</title>
- <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
- <style>
- #load
- {
- position: absolute;
- background-color: blue;
- color: #fff;
- padding: 10px;
- border: 1px solid #ccc;
- border-radius: 5px;
- width: 100%;
- height:100%;
- display:none;
- }
- #loadMe
- {
- background-color: blue;
- color: #fff;
- padding: 10px;
- border: 1px solid #ccc;
- border-radius: 5px;
- width: 80px;
- height:30px;
- cursor:pointer;
- }
- </style>
- <script>
- $(document).ready(function ()
- {
- $('#loadMe').click(function (e)
- {
- $('#load').show();
- $("#load").attr("src", "http://www.sibeeshpassion.com/");
- });
- });
- </script>
- </head>
- <body>
- <p id="loadMe">loadMe</p>
- <b>Load my website here.</b>
- <iframe id="load" src="" ></iframe>
- </body>
- </html>
Conclusion
I hope you enjoyed reading and found this useful. Please share me your valuable feedback. For me it matters a lot.

Nilesh JadavPosted Jun 13, 2015, 7:45 AM
Nice one sir !
Sibeesh VenuPosted Jun 13, 2015, 1:15 AM
Manoj Kulkarni Thank you
Sibeesh VenuPosted Jun 13, 2015, 1:15 AM
Santhakumar Munuswamy Thank you
Sibeesh VenuPosted Jun 13, 2015, 1:14 AM
Dominique Ceja Thank you
Manoj KulkarniPosted Jun 12, 2015, 11:06 PM
Nice Article
Santhakumar MunuswamyPosted Jun 12, 2015, 2:22 PM
Thanks for nice article :)
Dominique CejaPosted Jun 12, 2015, 9:30 AM
Nice work! Thanks
Sibeesh VenuPosted Jun 12, 2015, 8:57 AM
Rahul Saxena Thank you.
Sibeesh VenuPosted Jun 12, 2015, 8:57 AM
Pankaj Kumar Choudhary Thank you
Sibeesh VenuPosted Jun 12, 2015, 8:57 AM
Rakesh Chavda Thank you
Rahul Kumar SaxenaPosted Jun 12, 2015, 7:13 AM
Good show
Pankaj Kumar ChoudharyPosted Jun 12, 2015, 6:10 AM
Nice Article Sir..........
RakeshPosted Jun 12, 2015, 5:49 AM
Good one
Sibeesh VenuPosted Jun 12, 2015, 5:20 AM
Nitin Tyagi Thank you
NitinPosted Jun 12, 2015, 4:44 AM
nice