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, 3:45 AM EDT
Nice one sir !
Sibeesh VenuTOP 100Posted Jun 12, 2015, 9:15 PM EDT
Manoj Kulkarni Thank you
Sibeesh VenuTOP 100Posted Jun 12, 2015, 9:15 PM EDT
Santhakumar Munuswamy Thank you
Sibeesh VenuTOP 100Posted Jun 12, 2015, 9:14 PM EDT
Dominique Ceja Thank you
Manoj KulkarniPosted Jun 12, 2015, 7:06 PM EDT
Nice Article
Santhakumar MunuswamyPosted Jun 12, 2015, 10:22 AM EDT
Thanks for nice article :)
Dominique CejaPosted Jun 12, 2015, 5:30 AM EDT
Nice work! Thanks
Sibeesh VenuTOP 100Posted Jun 12, 2015, 4:57 AM EDT
Rahul Saxena Thank you.
Sibeesh VenuTOP 100Posted Jun 12, 2015, 4:57 AM EDT
Pankaj Kumar Choudhary Thank you
Sibeesh VenuTOP 100Posted Jun 12, 2015, 4:57 AM EDT
Rakesh Chavda Thank you
Rahul Kumar SaxenaTOP 100Posted Jun 12, 2015, 3:13 AM EDT
Good show
Pankaj Kumar ChoudharyTOP 100Posted Jun 12, 2015, 2:10 AM EDT
Nice Article Sir..........
RakeshPosted Jun 12, 2015, 1:49 AM EDT
Good one
Sibeesh VenuTOP 100Posted Jun 12, 2015, 1:20 AM EDT
Nitin Tyagi Thank you
NitinPosted Jun 12, 2015, 12:44 AM EDT
nice