hh54188
hh54188

Reputation: 15626

How to get a page's title which in iframe?

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?

Here the online case

Thank you

Upvotes: 3

Views: 2266

Answers (2)

Gautier ANTOINE
Gautier ANTOINE

Reputation: 9

see http://jsfiddle.net/TAtQT/7/

Upvotes: -1

Fr&#233;d&#233;ric Hamidi
Fr&#233;d&#233;ric Hamidi

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

Related Questions