最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论