VDN
VDN

Reputation: 85

How to get the href link from iframe

      my html content is in iframe.
        <iframe id="test" name=""src="test.html">
        .....content from test.html

      </iframe>

test.html(in my server) has a link like

         <div id="division"><a  href="www.google.com"></a></div>

jquery

              $(document).ready(function() {
           var value=$("#division").find('a').attr('href');
        alert(value);
                }

question:alert(value) returns undefined

Upvotes: 2

Views: 5730

Answers (1)

adeneo
adeneo

Reputation: 318192

Once the iframe has loaded, you'll have to use contents() to get the content, and then search and get the href property of the anchor, and the loaded content can't be cross domain, same origin policy etc :

$(document).ready(function() {
    $('#test').on('load', function() {
        var value = $(this).contents().find("#division a").prop('href');
        alert(value);
    });
});

Upvotes: 3

Related Questions