Search code examples
javascriptjqueryiframeelementreplay

Get html from element from iFrame every 10 seconds


So i've been looking around for a while for a possible solution to make a javascript, jquery which searches in the iFrame for a certain class or id element. Then takes out the html inside of it. (example. <a id="link">Here's a link</a> ). then makes it a string out of it and also replay this function every 5 second or so. Is there anyone who know a good solution to this or a tutorial?

I've tried the function var inf = $('#iframeid').contents().find('#theid').html();but it didn't gave any success.


Solution

  • Try this:

    function GetFrameData() {
        var $MyFrame = $("#iframeid");
    
        // You need to wait for the iFrame content to load first
        // So, that the click events work properly
        $MyFrame.load(function () {
            var frameBody = $MyFrame.contents().find('body');
            var inf = frameBody.find('#theid').html();
        });
    }
    
    // Call the function every 5 seconds
    setInterval(GetFrameData, 5000);