简体   繁体   English

如何使IFrame链接打开到新窗口?

[英]How to cause an IFrame link to open into a new window?

I have a website which is built using WP and uses SSL. 我有一个使用WP构建并使用SSL的网站。 On one of the page on my website, I have added an iframe call to another http website. 在我网站的一个页面上,我已将iframe调用添加到另一个http网站。

Here is my iframe call: 这是我的iframe电话:

<div class="embed-responsive embed-responsive-16by9">
<iframe src="//childwebsite.com/" target="_blank"></iframe>
</div>

The iframe is displayed properly. iframe正确显示。 Now when you click on anything inside the iframe, Chrome displays a message saying 现在,当您单击iframe中的任何内容时,Chrome会显示一条消息,提示您

Mixed Content: The page at ' https://parentwebsite.com ' was loaded over HTTPS, but requested an insecure resource ' http://childwebsite.com '. 混合内容:“ https://parentwebsite.com ”上的页面已通过HTTPS加载,但请求了不安全的资源“ http://childwebsite.com ”。 This request has been blocked; 该请求已被阻止; the content must be served over HTTPS. 内容必须通过HTTPS提供。

What I am looking for is when a user clicks inside an iframe, open a new tab in the browser and let the user be redirected to a particular on the childwebsite. 我正在寻找的是用户在iframe中单击,在浏览器中打开新标签并让用户重定向到子网站上的特定网站。

I tried adding target="_blank" to the iframe but it did not work. 我尝试将target =“ _ blank”添加到iframe,但是没有用。

I added the following JS also but it did not work 我也添加了以下JS,但没有用

//pass the iframe to this iframe getting function 
function iframeRef( frameRef ) {
    return frameRef.contentWindow ? frameRef.contentWindow.document : frameRef.contentDocument
}
//Get Iframe
var inside = iframeRef( document.getElementById('smugmugGallery') );
//Get all links
var links = inside.getElementsByTagName('input');
//Loop throught links and set their attributes
for (var i = 0 ; i<links.length ; i++){
    links[i].setAttribute('target','_blank');
}

Any help will be appreciated. 任何帮助将不胜感激。

You have two issues here. 您在这里有两个问题。 An SSL issue, and a cross domain issue. SSL问题和跨域问题。

  1. Your SSL issue can only be solved by serving the iframed content via SSL or serving the parent page via non-SSL. 您的SSL问题只能通过通过SSL提供iframed内容或通过非SSL服务提供父页面来解决。 This will remove the security alerts you are seeing. 这将删除您看到的安全警报。

  2. You cannot assert control over iframed content if it comes from another domain, well at least not easily for cross browser purposes. 如果内容来自另一个域,则无法断言对iframed内容的控制,至少出于跨浏览器的目的,这至少不容易。

From the Mozilla dev site: 在Mozilla开发人员网站上:

Scripts trying to access a frame's content are subject to the same-origin policy, and cannot access most of the properties in the other window object if it was loaded from a different domain. 试图访问框架内容的脚本应遵循同源策略,并且如果另一个窗口对象是从其他域加载的,则不能访问该窗口对象的大多数属性。 This also applies to a script inside a frame trying to access its parent window. 这也适用于试图访问其父窗口的框架内的脚本。 Cross-domain communication can still be achieved with window.postMessage. 仍然可以使用window.postMessage实现跨域通信。

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe#Scripting https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe#Scripting

Use base tag in iframe and try once. 在iframe中使用基本代码,然后尝试一次。

<base target="_blank" />

you can see more about Base tag here 您可以在此处查看有关Base标签的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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