Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Load a page in a dynamically generated iFrame when a link is clicked like a lightbox in jQuery?

I have a list of links eg :

<ul> 
<li><a href="http://google.com">Link1</a></li>
<li><a href="http://example.com">Link2</a></li>
<li><a href="http://example.com/sdf">Link3</a></li>
</ul>

When a link is clicked, a iFrame should be generated in the middle of the screen loading the page like a lightbox.

How to do this with jQuery ?

like image 253
Aakash Chakravarthy Avatar asked Nov 30 '25 04:11

Aakash Chakravarthy


1 Answers

$(document).ready(function(){
 $('a').bind('click', function(e){
    $('<iframe/>', {
       src:   $(this).attr('href'),
       class: 'myiframe',
       css:   {
           position: 'absolute',
           width:    '200px',
           height:   '200px',
           top:      window.innerHeight / 2 + 300,
           left:     window.innerWidth / 2 + 300
       }          
    }).appendTo(document.body);

    return(false);
});

$(document.body).bind('keydown', function(e){
  if(e.which === 27)
     $('.myiframe').remove();
});
});​

I poorly calculated the top/left coordinates there, you might want to replace that with a css class anyway. Use class: "classname" for that within creation.

References: .bind(), .attr(), .appendTo(), jQuery Core

like image 157
jAndy Avatar answered Dec 02 '25 17:12

jAndy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!