51

I want to load an iframe on click, this is what I have so far:

$("#frame").click(function () { 
      $('this').load("http://www.google.com/");
    });

It doesn't work. This is the complete code: JS Bin

Youss
  • 4,196
  • 12
  • 55
  • 109
  • 1
    Do you really have to use iframes ? I am sure someone will help you how to get this done, but you should avoid iframes as much as you can. –  Aug 24 '11 at 14:28
  • http://stackoverflow.com/questions/362730/are-iframes-considered-bad-practice/362743#362743 –  Aug 24 '11 at 14:29
  • I really need to use iframes:) – Youss Aug 24 '11 at 15:44
  • Alright then, I just mentioned so that you are doing it right, glade you found the right answer. –  Aug 24 '11 at 16:21

5 Answers5

109
$("#button").click(function () { 
    $("#frame").attr("src", "http://www.example.com/");
});

HTML:

 <div id="mydiv">
     <iframe id="frame" src="" width="100%" height="300">
     </iframe>
 </div>
 <button id="button">Load</button>
robsch
  • 9,358
  • 9
  • 63
  • 104
Dogbert
  • 212,659
  • 41
  • 396
  • 397
  • 7
    Cross Browser restrictions wont let you do this. –  Jul 04 '13 at 22:33
  • how would you gracefully handle errors? For example we have a report engine that generates a PDF document which we load into a hidden iframe which should automatically get the user to download it, but there's the possibility that something will go wrong and we'd like to notify the user of the problem. – Jacques Aug 15 '14 at 08:43
  • @Jacques: Write your own logic for your custom requirements. Above answer is perfect for OP's question. – Jitendra Pancholi Mar 07 '18 at 14:14
  • @Jacques How did you solve that problem ? I'm facing the same issue you've described – Jason Krs May 18 '18 at 10:48
  • @JasonKrs Unfortunately I don't recall since this was quite a while ago. Sorry I can't be of any help. – Jacques May 18 '18 at 14:02
  • @Jacques ouch... It's bugging me right now... Thank though – Jason Krs May 18 '18 at 16:47
  • @Dogbert I saw that answer already... I think since my frame is hosted on an Apache server, it is loaded with the 404 page... So, if the resource on the remote server is not yet available, I still get some content in `load`... Also, CORS blocked from exploring the Ajax features .... Still working on it... – Jason Krs May 19 '18 at 18:00
10

Try $(this).load("/file_name.html");. This method targets a local file.

You can also target remote files (on another domain) take a look at: http://en.wikipedia.org/wiki/Same_origin_policy

Chris
  • 3,729
  • 22
  • 30
9
$("#frame").click(function () { 
    this.src="http://www.google.com/";
});

Sometimes plain JavaScript is even cooler and faster than jQuery ;-)

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Christophe
  • 27,383
  • 28
  • 97
  • 140
5

here is Iframe in view:

<iframe class="img-responsive" id="ifmReport" width="1090" height="1200" >

    </iframe>

Load it in script:

 $('#ifmReport').attr('src', '/ReportViewer/ReportViewer.aspx');
Luqman Cheema
  • 409
  • 6
  • 5
1

Just in case anyone still stumbles upon this old question:

The code was theoretically almost correct in a sense, the problem was the use of $('this') instead of $(this), therefore telling jQuery to look for a tag.

$(document).ready(function(){
  $("#frame").click(function () { 
    $(this).load("http://www.google.com/");
  });
});

The script itself woudln't work as it is right now though because the load() function itself is an AJAX function, and google does not seem to specifically allow the use of loading this page with AJAX, but this method should be easy to use in order to load pages from your own domain by using relative paths.

Paul Ghiran
  • 1,233
  • 1
  • 16
  • 29