Reputation: 16011
Can I modify an iframe's anchor tags with javascript / jquery? I want to modify all anchor tags within the iframe to add a target="_blank"
Upvotes: 0
Views: 258
Reputation: 1256
Assuming the location of the document referenced in the iFrame is in the same domain as the parent document, you can access the anchor tags with this line of JavaScript:
{reference to iframe}.contentWindow.document.getElementsByTagName("a");
That will return an array, and you can then loop through it and change all the target attributes to "_blank".
I don't think you can do this with a 3rd-party URL because of cross-domain security issues.
Upvotes: 1
Reputation: 114347
If the Iframe src is not in the same domain as the parent page, you can't because of the same origin policy.
Otherwise, you can do this:
$("#iFrameName").contents().find("a").attr('target','_blank')
Upvotes: 2