CodeMonkey1313
CodeMonkey1313

Reputation: 16011

modify iframe hyperlinks

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

Answers (2)

Tim Hettler
Tim Hettler

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

Diodeus - James MacFarlane
Diodeus - James MacFarlane

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

Related Questions