Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make <iframes> load only on a button click?

Tags:

jquery

iframe

Friends, My issue is that my page is really slow to load, because all of the load simeltaneously. I am pulling an RSS feed that if you click on the "article" button it will pop up a Modal that has the inside of it. The problem is that all of the s load simeltaneously.

here is the site: http://daisy.camorada.com/

I have tried inserting a src into the but that doesn't work because i guess the iframe loads when the page opens.

<button class='btn btn-primary' id='miguel'>TEST ME NOW</button>
        <button class='btn btn-primary' id='jamison'>jamison</button>
        <div id="yoyoyo">
            </div>
            <iframe id="gogo">
                </iframe>

<script>
        $('#miguel').on('click', function() {
            alert('clicked');           
            $('#gogogo').attr('href', 'http://www.google.com');

        });
    </script>
    <script>
        $('#jamison').on('click', function() {
            alert('clicked JAMISON');       
            $("#yoyoyo").html("<h1>HELLO J</h1><br><iframe class='full-iframe' href='http://news.yahoo.com/three-killed-tribal-clash-libya-001717827.html'></iframe>");

        });

    </script>
like image 606
Alex Spencer Avatar asked Aug 24 '12 02:08

Alex Spencer


People also ask

How do you put only a certain section of a website into an iframe?

The most direct way to do what you want is to have your server give you a complete page that only contains the fragment you want to show. As an alternative, you could just use a simple <div> and use the jQuery load function to load the whole page and pluck out just the section you want: $('#target-div').

Are IFrames considered bad practice?

Iframes Bring Security Risks. If you create an iframe, your site becomes vulnerable to cross-site attacks. You may get a submittable malicious web form, phishing your users' personal data. A malicious user can run a plug-in.

How do I make an iframe PopUp?

Find there an insert “Add new PopUp” and choose Iframe template. Type the name of future Popup and click the button “Save”. Go to Design tab -> Appearance -> find “IFrame URL” field. Copy and paste here URL of site, that you need to display in PopUp as iFrame.


2 Answers

<a href="http://example.com" target="myiFrame">Click me!</a>
<iframe name="myiFrame" src="about:blank">

No JavaScript required.

like image 53
Sista Fiolen Avatar answered Oct 24 '22 07:10

Sista Fiolen


Html:

<iframe id="myiFrame" data-src="http://my.url" src="about:blank">
</iframe>

In your jQuery:

$("button").click(function(){
    var iframe = $("#myiFrame");
    iframe.attr("src", iframe.data("src")); 
});

Or if you wanted to load ALL iframes when you click one button... make sure they all have a data-src="your.url" attribute, and then loop through like this:

$("button").click(function(){
    $("iframe").each(function(){
        $(this).attr("src", $(this).data("src"));
    });
});
like image 21
ahren Avatar answered Oct 24 '22 06:10

ahren