Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How extract links from iframe using javascript

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
like image 644
hugo Avatar asked Apr 24 '26 13:04

hugo


2 Answers

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.

like image 123
alex Avatar answered Apr 26 '26 05:04

alex


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.

like image 20
AlienWebguy Avatar answered Apr 26 '26 04:04

AlienWebguy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!