Get iframe contents on load using live()

2020-03-30 04:24发布

I've got a file upload script working with an iframe so the page doesn't have to reload.

I've run into an issue concerning retrieving data from the iframe when it's loaded using jQuery's .live() event, like this:

$("iframe").live("load", function() {
    console.log($(this).contents().find("body").html());
});

This event doesn't even fire (I get nothing in Firebug), so how can I go about getting the HTML of an iframe after it's loaded some content from my file upload script? I want to do this so I can get the return state of the PHP script run on the server.

I have to use .live() as these elements are dynamically added, meaning the traditional .load() won't work.

标签: jquery iframe
1条回答
可以哭但决不认输i
2楼-- · 2020-03-30 05:00

From jQuery documentation:

Note: The .live() and .delegate() methods cannot be used to detect the load event of an iframe. The load event does not correctly bubble up the parent document and the event.target isn't set by Firefox, IE9 or Chrome, which is required to do event delegation.

http://api.jquery.com/load-event/

Can you provide a little more information on what you're trying to do, it should be possible to catch the event but not by using iFrame load.

查看更多
登录 后发表回答