简体   繁体   中英

Open iFrame links in a new tab?

I have endlessly tried to open iframe links inside either the parent tab or a new tab however I just can't seem to get them to work. When I set the links to target="_parent" the links do nothing and when I set the links to target="_blank" they open in a new tab but also display nothing.

As soon as I get rid of the code that adds the target to the links they work but open inside the iframe which is not what I am trying to accomplish. Any help would be greatly appreciated.

Here is the iframe code that I am using:

<iframe src="js/eventsFrame2.php?EventID=2422308" id="frameclass" frameborder="0" scrolling="no" width="100%" height="600">

Here is the code inside the iframe:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
  function myFunction() {
    $("a").attr('target','_parent');

  }
$(window).load(myFunction);

</script>

<script language="javascript" type="text/javascript"> 
    document.write('<script language="javascript" src="http://tickettransaction.com/?bid=1761&amp;sitenumber=18&amp;tid=ticket_results&amp;evtid=2422308"><\/script>');
</script>

Have you tried overwriting the parent page (containing the iframe) with your content?

Code inside the iframe:

<script language="javascript" type="text/javascript"> 
    parent.document.write('<script src="http://tickettransaction.com/?bid=1761&amp;sitenumber=18&amp;tid=ticket_results&amp;evtid=2422308"><\/script>');
</script>

Query defines the contents() method to grab the document node, but it doesn't give you a cross-browser way to go from the document to the window, so you're still stuck with:

var iframea = $('iframe').contents().find("a");

iframea.attr('target','_parent');

IMPORTANT: you should always check that the document is with the status "complete" for work with this

var iframe= document.getElementById('iframe_id');
//var iframewindow= iframe.contentWindow
//? iframe.contentWindow : iframe.contentDocument.defaultView;
var preview =  previewFrame.contentDocument ||  previewFrame.contentWindow.document;
if (preview.readyState=="complete")
{
   //ok
}
else
{
   //perform a recursive call until it is complete
}

If you have control over the content inside the iframe, you could try using the base tag.

Example -- In the page loaded into the iframe include a base tag within the head:

<base target="_top"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM