简体   繁体   中英

Forcing “http” links in an Iframe to open in new tab

I have a page hosted on https with iframe content that has http links within it. The framed content is on another domain but is https but I want to be able to make the http links open into a new tab. So far I have not been able to acheive this through jquery.

Example.

 <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $(function() { $("a[href^='http://']:not([href*='https://'])").each(function() { $(this).click(function(event) { event.preventDefault(); event.stopPropagation(); window.open(this.href, '_blank'); }).addClass('externalLink'); }); }); </script> </head> <body> <div class="banner"> <a href="http://example.com">WE ARE STILL HERE</a> <iframe name="home" scrolling="auto" height="100%" width="100%" noresize="true" src="https://www.example.com"> </iframe> </div> </body> </html> 

You can try this with various mixed content pages. You will notice the placeholder link "WE ARE STILL HERE" opens perfectly to a new window while anything in the iframe will not load without choosing to allow insecure content.

Any help will be greatly appreciated.

If iframe url and main page url are on the same domain, well it could be done by this code to access elements which are placed inside iframe

var iframe = document.getElementById('iframeId');
var innerDoc = iframe.contentDocument || iframe.contentWindow.document;

But if not actually there is not way

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