0

Say I have this text Click me, and I want a modal window, which contains an iframe to a website (eg., www.google.com) to come up whenever one clicks on the text.

How should I go about doing this? I did some googling and an example of the iframe

%iframe{:src => "http://www.google.com"}

But I am not sure how I could use it ...

Here's the structure of my view file (html.haml), I tried something like this (but it didn't work!):

...
%li
  = User_name
  = link_to 'Click me' %iframe{:src => "http://www.google.com"}
%li
...

Again, how do I open a popup when some one clicks on the 'Click me' text?

One Two Three
  • 22,327
  • 24
  • 73
  • 114
  • you aren't sure how to handle the js to render your iframe? and do you mean an actual dialog box or just in a pop-up or modal window? – Brad Feb 27 '13 at 19:40
  • Well, perhaps I shouldn't have been too specific about the dialog. Yes, I just want a popup window that appears inside the current window (not the kind that would open in new tab/window) – One Two Three Feb 27 '13 at 19:45
  • I'm also not sure what you want. Do you want an overlay to open? – vinczemarton Feb 27 '13 at 19:45
  • @SoonDead Do you use facebook? If so, do you notice when you click on 'Click to change photo' in your profile page, you'd get a popup window asking you to choose some photo to be your profile pic? ... I kind of want to do the same thing (It's just simple in my case in that I don't want a file chooser or anything, I just want a popup that contains on iframe) – One Two Three Feb 27 '13 at 19:47

3 Answers3

1

Use a link with its target attribute set to the value of the name attribute on your modal <iframe>:

<a href="http://www.bing.com" target="modal">Open Bing in an iframe</a>
<iframe src="#" name="modal"></iframe>

Then in your JS:

var modalTriggers = Array.prototype.slice.call(document.querySelectorAll('a[target=modal]'));
var modal = document.querySelector('iframe[name=modal]');

modalTriggers.forEach(function(trigger){
    trigger.addEventListener('click', function() {
        modal.classList.toggle('active');
    }, false);
});

And Some CSS:

iframe[name=modal] {
    display: none;
    width: 92%;
    height: 92%;
    position: fixed;
    top: 4%; left: 4%;
    background: white;
    box-shadow: 0 2px 12px 6px rgba(0,0,0,.6);
}

iframe[name=modal].active {
    display: block;
}

Demo (edit)

On MDN

Web_Designer
  • 72,308
  • 93
  • 206
  • 262
1

I am sure you do not want to use an iframe for displaying the content from a url, instead you want to shoe it in a pop up window. Yo can use fancybox-rails gem. after installing the gem as described in the readme add this in your view file

%li
  = User_name
  = link_to 'Click me', "http://www.google.com", :class => "iframe"

and this in your javascript file

$(document).ready(function() {
  $("a.iframe").fancybox();
});

You can read more about the uses here. There are many other jquery plugins available for the same.

Manoj Monga
  • 3,033
  • 14
  • 19
0

there are a couple ways to do this if i understand you correctly. this is a simple approach if you include jquery into your html

    <html>
    <head>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
    $('#clickMe').click(function(){
    $('#myIframe').show(); 
    });
    });
    </script>
    </head>
    <body>
    <input id="clickMe" name="clickMe" type="button"/>
    <iframe id="myIframe" style="display:none;" src="http://www.othersite.com/some/path?param1=value1&param2=value2">
        <p>Placeholder text; only shows up if the page DOESN'T render!</p>
    </iframe>
    </body>
    </html>

you'd also want to style your iframe to behave like a modal window...there are lots of tutorials on that.... as well as handle the closing behaviors

this is a more in depth explanation http://www.jacklmoore.com/notes/jquery-modal-tutorial

Brad
  • 6,106
  • 4
  • 31
  • 43