jquery - How extract links from iframe using javascript - Stack Overflow

Example:iframe.html<a href="">Google<a>bla bla bla<a href="">Y

Example:

iframe.html

<a href="">Google</a>
bla bla bla
<a href="">Yahoo</a>

index.html

<script>
...
</script>
There are the links from "iframe.html"


Example:

iframe.html

<a href="http://www.google.">Google</a>
bla bla bla
<a href="http://www.yahoo.">Yahoo</a>

index.html

<script>
...
</script>
There are the links from "iframe.html"
http://www.google.
http://www.yahoo.
Share Improve this question asked Jul 14, 2011 at 5:16 hugohugo 372 silver badges4 bronze badges 4
  • Is index.html and iframe.html in the same domain? – wong2 Commented Jul 14, 2011 at 5:21
  • Does iframe.html have the same domain, protocol and port as index.html? – alex Commented Jul 14, 2011 at 5:22
  • See this answer. detect url's in text with Javascript – Adeel Commented Jul 14, 2011 at 5:22
  • @Adeel: That is detecting URLs in strings. The OP wants links in the DOM. – alex Commented Jul 14, 2011 at 5:31
Add a ment  | 

2 Answers 2

Reset to default 4

If the domain, protocol and ports match, just use...

var links = $('iframe:first').contents()[0].links;

jsFiddle.

...or without jQuery...

var iframe = document.getElementsByTagName('iframe')[0],
    doc = iframe.contentDocument || iframe.contentWindow.document; 

var links = doc.links;

jsFiddle.

This takes advantage of the document.links property.

Assuming your iframe is on the same domain as your website, and has the id "my_iframe" and you have a div with the id "results", this should work for you:

$("#my_iframe").contents().find('a').each({
    $('#results').append($(this).attr('href') + '<br />');
});

Take a moment to read up on JQuery's .contents() function.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745367143a4624635.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信