Skip to content Skip to sidebar Skip to footer

Programmatically Click Link In Iframe With Javascript

Here's my html/javascript. Below there are two if statements that I've tried. I used indexof to get the text between the tags. Also tried href == to use the hre

Solution 1:

From your comment, you are loading the content of different domain into you iframe, so you just can't read the content of that domain due to same origin policy restrictions.


Solution 2:

Here's a demo of the issue, code mostly copied & pasted from your question:

http://jsfiddle.net/colllin/G2BDb/

And the inner iframe code is here:

http://jsfiddle.net/colllin/wc8Up/

As you can see in the console when you click the button,

Refused to display 'https://www.google.com/#Flag' in a frame because
    it set 'X-Frame-Options' to 'SAMEORIGIN'.

So it looks like if you have control over the page that the iframe is linking to, you could set X-Frame-Options header on that page to be specify an ALLOW-FROM uri (from MDN):

ALLOW-FROM uri
  The page can only be displayed in a frame on the specified origin.

Alternatively if you have control over the original iframe source (which I assume you do, otherwise it's impossible to even access the contents, let alone click a link), you could add the target="_top" attribute to the link before clicking it. This would cause the linked page to replace your top-level page in the window.

Demo here: http://jsfiddle.net/colllin/G2BDb/2/


Post a Comment for "Programmatically Click Link In Iframe With Javascript"