8

Given the following HTML, is it possible for the anchor tag in the iframe to target the parent window?

<div>
  <iframe src="/pageview.html"></iframe>
</div>

iframe (pageview.html) content :

<a href="http://www.google.com">link</a>
2
  • You can't update the contents of the <iframe> without JavaScript. It also doesn't make sense for the iframe to have content Feb 19, 2013 at 0:50
  • @ExplosionPills that's just an anecdotal rendering of what might load in the iframe from the src. Right now, as is usual, the anchor tag opens within the iframe when clicked. I'm just wondering if there's a way to change that behavior
    – Wesley
    Feb 19, 2013 at 0:53

2 Answers 2

14

You want the target attribute:

<a href="something" target="_parent">go</a>

Using _parent will target the frame's immediate parent window. To target the top window, use _top.

2
<div>
  <iframe src="/pageview.html">
    <a onclick="javascript:window.parent.location.href='http://www.google.com'; return false;">link</a>
  </iframe>
</div>

Should be something like that...

4
  • You should mark as answer if this solves the problem @Wesley. Feb 19, 2013 at 0:59
  • I will, there is a time delay on when you can mark an answer.
    – Wesley
    Feb 19, 2013 at 1:00
  • 2
    This is not a good use of JavaScript. You are simulating built-in browser behavior that has worked since the 1990's with JavaScript. What if the user has turned off JavaScript? Meanwhile, you eliminate the href attribute, which has implications for CSS, keyboard navigation, accessibility, and SEO. And, just to nit-pick, you should not use the javascript: prefix in an onclick attribute. Use target="_parent" instead. It is cross-browser, and semantically correct. This is exactly what the target attribute is for.
    – gilly3
    Feb 19, 2013 at 1:00
  • fair enough....I was kind of assuming this was answering more of a canonical example for someone who was eventually going to do it in JQuery programatically. I'm upvoting your ans
    – rnirnber
    Feb 19, 2013 at 1:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.