读取iframe内容并将其存储在变量中

时间:2012-06-27 14:24:39

标签: javascript jquery iframe

我有一个iframe,可以说它包含以下文字:

<h1> Hello World</h1><h2>Good Morning</h2>
<h3>Try this http://www.google.com</h3></body></html>

我想阅读上面找到的链接并访问它。任何的想法?我不能使用控制台,因为我需要从这个页面获取cookie

2 个答案:

答案 0 :(得分:0)

除非iframe与父页面属于同一个域,否则无法执行此操作。

如果是,则可以使用以下脚本:

var link = $('#iframeid').contents().find('h3').html(); //Thanks to rob for the correction

然后你只需要从变量中过滤出其他文本的链接。

答案 1 :(得分:0)

获取iframe的内容,即使它在您的域外:

var link = $('#MyiFrame').contents().find('h3').text();
//now parse the link variable to what you need, maybe split it at :// 
//and the following space afterwards
祝你好运!