jQuery onload - .load() - event not working with a dynamically loaded iframe

后端 未结 4 1722
忘了有多久
忘了有多久 2021-01-02 00:15

My script loads an iframe when the user clicks a button. I want to call another function after the iframe loads, but it\'s not working. The iframe does load normally, but

4条回答
  •  别那么骄傲
    2021-01-02 00:24

    Seems somewhat similiar to the question here:

    jQuery .ready in a dynamically inserted iframe

    This way the Iframe element exists in the Dom with the append. Only when you set the Url will it load, by which point you've attached the load function you wish to tag on the end. So for your code

    $('#someButton').live('click', function () {
    
        $('body').append($(''));
        $('#myIframe').attr('src',"https://www.mywebsite.com");
    
        $('#myIframe').load(function () {
            alert('iframe loaded');
        });
    
    });
    

提交回复
热议问题