0

I have an external iframe. Inside that iframe i search for a specific link. I want to know the link href. The following code seems to find me the a element. But i get undefined for the attr href.

Finding the a element in the iframe which seems to work, i get an element:

$('.content-7232342').contents().find('body > a:first-of-type')

Trying to get the href attribute results in undefined:

$('.content-7232342').contents().find('body > a:first-of-type').attr('href')
Dapp Future
  • 165
  • 1
  • 12
  • Does this help? https://stackoverflow.com/questions/1600488/calling-javascript-function-in-iframe – evolutionxbox Jun 17 '21 at 10:27
  • @evolutionxbox no – Dapp Future Jun 17 '21 at 10:37
  • Is the content of the iframe hosted on a different domain to the parent window? – Rory McCrossan Jun 17 '21 at 10:53
  • @RoryMcCrossan Yes it's a external website. – Dapp Future Jun 17 '21 at 11:00
  • 1
    Unfortunately in that case what you're trying to do is not possible. Browsers are prevented from accessing/reading/updating the DOM of a cross-domain iframe for security reasons (in order to prevent spoofing, phishing etc. attacks) – Rory McCrossan Jun 17 '21 at 11:07
  • @RoryMcCrossan But the iframe content is in my DOM, why can't i read the content in my DOM even if it's from a iframe, i don't want to add content, only read what is already in my DOM. – Dapp Future Jun 17 '21 at 11:18
  • 1
    That's the problem - it's not in your DOM. The content of the iframe is rendered in it's own DOM which is not accessible to the outer page for the reasons I described in my previous comment. If you use devtools to inspect the page structure you will see the separate DOMs listed. – Rory McCrossan Jun 17 '21 at 11:21

0 Answers0