1

I want access to the content of an iFrame, on the same domain. I have a small and easy example but it doesnt' work, I search the problem since too much time :-(

Here is my code :

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
<head>
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script>
        $(document).ready(function(){   
            alert($('#iframeID').contents().find('#someID').html());
        });
    </script>
 </head>
<body>  
    <iframe id="iframeID" src="test.html"></iframe>
</body>
</html>

And the frame called :

<div id="someID">Hello world!</div>

Can someone help me please ? Thank you for your help

Mathieu

2 Answers 2

1

Get iframe as

console.log($('#iframeID', window.parent.document));

You need to wrap code inside of window.load or need to wrap it inside .load() because your iframe contents are not getting load before code is executing.

<script>
    $(window).load(function(){   
        //alert($('#iframeID').contents().find('#someID').html());
        console.log($("#iframeID", window.parent.document).contents().find("div"));
    });
</script>
Sign up to request clarification or add additional context in comments.

Comments

1

Use .load() to make sure the iframe is loaded before searching inside.

$(document).ready(function() {
    $("#iframeID").load(function(){   
        alert($(this).contents().find('#someID').html());
    });
});

1 Comment

Ohhhhhhhhhh :-) :-) That works Thank you so much for your quick help !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.