Get title string from url within iframe using jQuery?

旧城冷巷雨未停 提交于 2021-02-05 09:30:31

问题


I wonder if it is possible to get the page title of an url within IFRAME. I tried to use jQuery $('#iframe').attr('title') but that doesn't work. Please help. Thanks.


回答1:


I think this is what you're looking for:

$(document).ready(function()
{
    alert($('#iframe').contents().find("title").text());
});

That will return the page title that is located in the <title> tag of the iframe document that has the id "iframe".




回答2:


I misread your question.

$('title', frames[frameName].document).text();



回答3:


If what you are looking for is a way to get the URL of the iframe after the user has clicked on a link within that iframe, this is not possible in any modern browser that blocks cross-domain attempts.

Even though the iframe is part of the DOM and you can easily find the new iframe URL using apps like Firebug, Firefox will throw a XSS error on any attempts by js to directly pull that info.

But for the record, as it's already been said, the location within the DOM of the actual URL of the iframe content is (with a little help from jquery) : $("#id_of_iframe).contentDocument.location.href

I'm not totally sure if the above points straight to it with the above syntax, but that's the gist of it. The part that is a no-no is trying to go inside that contentDocument part.



来源:https://stackoverflow.com/questions/1319880/get-title-string-from-url-within-iframe-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!