tags:

views:

40

answers:

2

How can I reload iframe using jquery?

//Reload Iframe Function

Reload

Thanks in Advance

A: 
$("#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

Haim Evgi
EarnWhileLearn
i update but see @cory answer is good for you
Haim Evgi
A: 

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"&gt;&lt;/script&gt;
</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"&gt;&lt;/script&gt;
</head>
<body>

    <iframe id="frame" src="file:///D:/Users/Cory Larson/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.

Cory Larson
EarnWhileLearn
You might want to post large comments like these as updates to your original post. Please see my update on my answer for a working example.
Cory Larson