user2203703
user2203703

Reputation: 2005

Load iframe links into parent window?

I have this iframe code:

<iframe src="http://www.example.com" border="0" framespacing="0" marginheight="0" marginwidth="0" vspace="0" hspace="0" scrolling="yes" width="100%" frameborder="0" height="100%"></iframe>

What i'm trying to do is..when i click any link in iframe page..then i will go to it and the page will load and i will getout from the current page to the iframe link as a new page.

Something like target="_self" - not like the normal iframe which will go to this link without any changes in browser itself.

Example : iframe src : http://www.w3schools.com

When i click "Learn HTML" inside the iframe page..then i will go to http://www.w3schools.com/html/default.asp and my browser URL will change to it too.

So i just clicked a link inside the iframe and i go to it.

Any ideas?

Upvotes: 31

Views: 65019

Answers (6)

danieltan95
danieltan95

Reputation: 850

Working solution for mobile safari and all other sane browsers, through javascript.

Safari iframes block opening links, and if your iframes are nested with no control over them (e.g. loaded as a vendor etc), this is a working method.

// https only
var redirLink = "https://www.google.com";
// create an anchor and simulate a click.
var a = document.createElement('a');
a.title = redirLink;
a.href = redirLink;
// only "_top" or "_parent" supported.
a.target = "_top"; 
// prevent the default behaviour of anchors and do the address change manually. This must be tied to user actions to work.
a.onclick="function(e){e.preventDefault();top.location.href=this.href}"
document.body.appendChild(a);
a.click();

References:

  1. How can I open a link in a nested iframe in the original window they're placed in?

Upvotes: -1

Sandeep Sherpur
Sandeep Sherpur

Reputation: 2802

If site opens in iframe then redirect to main site

<script>
    if (window.top.location != window.self.location) {
        top.window.location.href = window.self.location;
    }
</script>

Upvotes: 3

Ismael Serrano
Ismael Serrano

Reputation: 71

You can use javascript to add the target to the links dynamically, something like this.

function onLoadIF(frame)
{
    var elements = frame.getElementsByTagName('a'),
        len = elements.length;

    while( len-- ) {
        elements[len].target = "_parent";
    }
}

And then add onload="onLoadIF(this)" to the iframe.

Upvotes: 7

dsgriffin
dsgriffin

Reputation: 68566

Yes, you can use target="_parent" to achieve this.

"target="_parent" opens the linked document in the parent frame."

Example:

<a target="_parent" href="http://example.org">Click me!</a>

Edit:

If that's not working, you can try _top:

<a target="_top" href="http://example.org">Click me!</a>

Or base target:

<base target="_parent" />

Or window.top.location in JavaScript:

window.top.location = "http://example.com";

Upvotes: 42

Adam Plocher
Adam Plocher

Reputation: 14233

What you're looking for is <a href="..." target="_parent">

_parent will cause it to go to the parent frame

_top will cause it to go to the top-most level.

Either _parent or _top should work for your target.

Upvotes: 17

Skatox
Skatox

Reputation: 4284

Yes, just add target="_parent" on the link and it load on main page.

Upvotes: 5

Related Questions