Reputation: 15626
first I have a simple iframe
<iframe src="http://www.google.com"></iframe>
I also have a button to alert the google's title
<input type="button" value="gettitle" />
then I want use jquery to get the iframe inner html's title
$("input ").click(function () {
alert($('iframe').contents().find("title").text());
});
But it doesn't work,what's wrong with this code?How can I get the title correctly?
Thank you
Upvotes: 3
Views: 2266
Reputation: 262939
Same origin policy will prevent you from doing that if the page in your <iframe>
element doesn't come from the same domain as your main page. Since your example uses www.google.com
, it won't work.
That said, your code is correct and should work as expected when the two pages come from the same domain.
Upvotes: 5