How to Force Link from Iframe to Be Opened in the Parent Window

How to force link from iframe to be opened in the parent window

I found the best solution was to use the base tag. Add the following to the head of the page in the iframe:

<base target="_parent">

This will load all links on the page in the parent window. If you want your links to load in a new window, use:

<base target="_blank">

Browser Support

What causes an iframe to open new links inside parent window?

<a href="http://en.wikipedia.org" target="_top">Top</a>

<a href="http://en.wikipedia.org" target="_self">Self</a>

The magic of target: http://jsfiddle.net/DerekL/wxcufehg/

how to force link from iframe to be opened in the parent window when iframe is not from same domain?

You can try the code from this post:
How to add click event to a iframe with JQuery

I think may You can use the .live() jquery function but may be U cannot :) I browse in internet about this case and .live() doesn't work, but .bind() must work fine :)

How to make all links in an iframe open in new tab

I'd expect this :

$('a').setAttribute('target','_blank');

.. to fail (silently - because jquery generally fails silently) because, if the content of the iFrame is from a different Domain, there are access issues with manipulating the page within the iFrame from the containing page.

That is : If your own page is from domain a (eg mysite.com), and the iframe is from domain b (eg someothersite.com), then web browser security behaviour is such that using javascript to manipulate the iFrame content will give an "Access is denied" error.

If you were to use non-jquery javascript you'll see the error. From the parent page, something likie this :

window.frames["iFrameName"].getElementsByTagName("a")[0].target="_blank"

you'll see the error.

Unfortunately I'm not sure what you can do about this. It's a deliberate thing to stop one website including another, and changing the content after loading so that it says something else.

One way around it would be to call a script on your server (ie same domain) and pass in a URL. The script gets the content of the page you were after and regurgitates it to your browser, so you'll have the content of your desired webpage but the address of it was within your site.. eg if it were php:

http://yourdomain.com/getURL.php?url=http:www.google.com

Watch out for issues with authentication there though



Related Topics



Leave a reply



Submit