最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Catch iframe reload with jQuery - Stack Overflow

programmeradmin1浏览0评论

I have an iframe loaded dynamically with jQuery like this

jQuery('<iframe id="myFrame" src="iframesrc.php"></iframe>').load(function(){
    // do stuff when loaded
}).prependTo("#myDiv");

And I want to catch the reload event every time the inner frame is reloaded. I tried this:

jQuery('body').on('load', '#myDiv', function() {
    alert('iframe loaded');
});

But I'm not getting any response, both on the initial load and when following links inside the iframe. What am I doing wrong?

I have an iframe loaded dynamically with jQuery like this

jQuery('<iframe id="myFrame" src="iframesrc.php"></iframe>').load(function(){
    // do stuff when loaded
}).prependTo("#myDiv");

And I want to catch the reload event every time the inner frame is reloaded. I tried this:

jQuery('body').on('load', '#myDiv', function() {
    alert('iframe loaded');
});

But I'm not getting any response, both on the initial load and when following links inside the iframe. What am I doing wrong?

Share Improve this question asked Jul 11, 2012 at 12:15 LobsterManLobsterMan 1,2081 gold badge13 silver badges20 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 18

You are looking for the load action of a div in your example above, not the iframe. Try:

$("#myFrame").on("load", function () {
    alert("Hi there, hello");
})

Additionally, if you are not using other libraries, use $() to access jQuery as opposed to jQuery()

Also note that any functions that you want to run on your page must be bound to jQuery's document ready event like so:

$(function () {
    // your code goes here
    $("#myFrame").attr("src", "http://google.com"); // <- this works
})

$("#myFrame").attr("src", "http://google.com"); // <- this does not work

for new url

location.assign("http:google.com");

The assign() method loads a new document.

reload

location.reload();

The reload() method is used to reload the current document.

发布评论

评论列表(0)

  1. 暂无评论