9

How to Load iframe content in Popup div. popup div will be open on click of each link from each link, page url will load to iframe href inside popup div.

$(document).ready(function(){
  $(".openpop").click(function(){
    var x =    $(this).attr('href');
    alert(x);
    y =  x.replace('#', '');
    alert(y);
    $(".popup").toggle();
    $("iframe").attr("href", y);

  });

   $(".close").click(function(){
     $(this).parent().fadeOut("slow");
  }); 

});

HTML

<a class="openpop" href="#http://www.google.com">Link 1</a>
<a class="openpop" href="#http://www.yahoo.com">Link 2</a>
<a class="openpop" href="#http://www.w3schools.com">Link 3</a>
<div class="wrapper">
<div class="popup">
<iframe src="">
  <p>Your browser does not support iframes.</p>
</iframe>
<a href="#" class="close">X</a></div>
</div>
PRANAV
  • 1,009
  • 5
  • 16
  • 36
  • would be nice to get a jsfiddle for better understanding – Dwza Sep 11 '14 at 07:43
  • There is no href on iframe, it should be src attribute, $("iframe").attr("src", y); and then x-frame-options will bother it. – SSA Sep 11 '14 at 07:43

3 Answers3

12

First, you don't need # in the link. Just call e.preventDefault(); to stop the link from executing.

For security reasons you can't open every link eg. google.

Also you might not use toggle because you allways have to click it twice if a frame is opend already.

here is a working fiddle

html

<div class="links">
    <a class="openpop" href="http://getbootstrap.com/">Link 1</a>
    <a class="openpop" href="http://www.jsfiddle.net">Link 2</a>
    <a class="openpop" href="http://www.w3schools.com">Link 3</a>
</div>
<div class="wrapper">
    <div class="popup">
        <iframe src="">
            <p>Your browser does not support iframes.</p>
        </iframe>
<a href="#" class="close">X</a>
    </div>
</div>

jquery

$(document).ready(function () {
    $(".popup").hide();
    $(".openpop").click(function (e) {
        e.preventDefault();
        $("iframe").attr("src", $(this).attr('href'));
        $(".links").fadeOut('slow');
        $(".popup").fadeIn('slow');
    });

    $(".close").click(function () {
        $(this).parent().fadeOut("slow");
        $(".links").fadeIn("slow");
    });
});

of course you have to do some changes in styling for better view experience :)

Dwza
  • 6,494
  • 6
  • 41
  • 73
0
$(document).ready(function() {
    $(".openpop").click(function() {
        var x = $(this).attr('href');
        var y = x.replace('#', '');
        $(".popup").show().html('<iframe src="'+y+'"></iframe>');
        $("iframe").attr("href", y);
    });

    $(".close").click(function(){
        $(this).parent().fadeOut("slow");
    }); 
});

you can not open google and yahoo in iframe both site don't allow for it please see this link Why Iframe dosen't work for yahoo.com

but you can open w3school with this code

Dwza
  • 6,494
  • 6
  • 41
  • 73
Ankit Agrawal
  • 6,034
  • 6
  • 25
  • 49
  • Hi, it will not work this way.. Load denied by X-Frame-Options: https://in.yahoo.com/?p=us does not permit framing. Console error for both Google and Yahoo – Subh Sep 11 '14 at 07:49
0

You can also use this

$('#clickme').click( function(){
    $('#showDiv').show();
    $('#iframe').attr('src','your url');
});
mindriot
  • 5,413
  • 1
  • 25
  • 34
mithunSalinda
  • 300
  • 3
  • 7