Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax Reload iframe

Tags:

jquery

ajax

How can I reload iframe using jquery?

<html>

<head>
<script type="text/javascript" src="jquery-1.4.2.js" > </script>


<script type="text/javascript">
//Reload Iframe Function    
</script>

</head>

<body>
    <iframe name="f1" id = "f1" src="http://www.google.com.pk/search?q=usa+current+time&ie=utf-8&oe=utf-8&aq=t&client=firefox-a&rlz=1R1GGLL_en" width=500px height=250px>
    </iframe>

<button onClick="abc()"> Reload </button>

</body>

</html>
like image 903
Novice Avatar asked Aug 24 '10 11:08

Novice


2 Answers

Based on your comment to Haim's post, what you want could be simplified as:

$('#reload').click(function() {
    $('#f1').attr('src', $('#f1').attr('src'));
    return false;
});

Or

$('#reload').click(function() {
    $('#f1')[0].contentWindow.location.reload(true);
    return false;
});

But perhaps his answers better suits your needs.

UPDATE:

I put together a working example using the method I provided above. I have an <iframe> in one page that shows another page that prints the time.

The <iframe> HTML (time.html):

<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
<body>

    The time is now <span id="time"></span>.

    <script type="text/javascript">

        $(document).ready(function() {
            $('#time').html((new Date()).toString());
        });

    </script>
</body>

And the parent page HTML (test.html):

<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
<body>

    <iframe id="frame" src="file:///D:/Users/Cory/Desktop/time.html" width="500" height="250"></iframe>

    <button id="reload">Refresh</button>

    <script type="text/javascript">

        $(document).ready(function() {
            $('#reload').click(function() {
                $('#frame').attr('src', $('#frame').attr('src'));
                return false;
            });
        });

    </script>
</body>

This works just fine for me in Firefox 3.6 and IE 7.

like image 146
Cᴏʀʏ Avatar answered Sep 19 '22 04:09

Cᴏʀʏ


$("#reload").click(function() {
            jQuery.each($("iframe"), function() {
                $(this).attr({
                    src: $(this).attr("src")
                });
            });
            return false;
        });

This will loop through each iFrame every time you click on a link with a class of reloadAds and set the source of the iFrame to itself

like image 33
Haim Evgi Avatar answered Sep 19 '22 04:09

Haim Evgi