Reputation: 4862
How to access div element by jquery on iframe?
I tried this (jsFiddle)
<script>
var ifr = $("#ifr");
$("#ifr").load(function () {
alert($("#ifr").contents().find('#test').html());
});
$("#ifr").attr('src', 'http://meoooh.raonnet.com/test/oh.html');
</script>
<iframe id="ifr" width="560" height="315"></iframe>
And
I get a undefined
alert.
Why I can't access div element?
Upvotes: 0
Views: 195
Reputation: 1
Chrome returns the following error in the console:
Blocked a frame with origin "http://fiddle.jshell.net" from accessing a frame with origin "http://meoooh.raonnet.com". Protocols, domains, and ports must match.
Upvotes: 0
Reputation: 1039588
Why I can't access div element?
For security reasons. You can only access DOM elements of a child iframe if this iframe is pointing to the same domain as the parent page. In the example you have shown the parent page is hosted on jsfiddle.net
whereas the src
property of your iframe if pointing to meoooh.raonnet.com
which are 2 different domains and browsers do not allow you to do that. Only if the 2 domains are the same you can access elements inside the iframe.
Upvotes: 1