开发者

How extract links from iframe using javascript

开发者 https://www.devze.com 2023-03-20 08:55 出处:网络
Example: iframe.html <a href=\"http://www.google.com\">Google</a> bla bla bla <a href=\"http://www.yahoo.com开发者_开发百科\">Yahoo</a>

Example:

iframe.html

<a href="http://www.google.com">Google</a>
bla bla bla
<a href="http://www.yahoo.com开发者_开发百科">Yahoo</a>

index.html

<script>
...
</script>
There are the links from "iframe.html"
http://www.google.com
http://www.yahoo.com


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

精彩评论

暂无评论...
验证码 换一张
取 消