简体   繁体   中英

When opening iframe content in a new tab, make it open in an iframe that sits inside a page

I have various pages that open inside an iframe. I want to avoid the scenario where if the user opens one of the pages in a new tab, it opens as a page by itself. Rather, I want the parent page to open in the new tab, with the iframe content embedded.

I have some javascript from an older site I built that works perfectly when doing this with frames. I didn't write the script, and am a little unsure how to update it. This script goes in the parent frame:

var fname="mainFrame";

window.onload=function(){
var d=document.location.search;
if(d!='')self.frames[fname].document.location.href=d.substring(d.lastIndexOf('?')+1,d.length);
}

And this goes in the child frame:

if(self.location==top.location)self.location="index.htm?article.htm";

How can this script be modified to work with an iframe? And given that I've been using the script for over a decade, is it still the best or most efficient method? I'm using Bootstrap, so don't mind a jQuery solution if it's better than the above javascript method.

Got it!

 window.onload=function(){ var ifrm = document.getElementById('newsframe'); var d=document.location.search; if(d!='')ifrm.src=d.substring(d.lastIndexOf('?')+1,d.length); } 

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