Javascript 如何从 iframe 打开一个新标签?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14856259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 18:09:50  来源:igfitidea点击:

How to Open a new tab from Iframe?

javascriptjqueryhtmliframebrowser

提问by Rahul Ukil

I have a website build in php, in some page i show a iframe with another website link, which displays it's login screen.

我有一个用 php 构建的网站,在某些页面中,我显示了一个带有另一个网站链接的 iframe,它显示了它的登录屏幕。

Both Websites are in different domain.

两个网站在不同的域中。

Like main website : http://www.abcd.comIframe href : http://www.xyz.com

喜欢主网站:http://www.abcd.com Iframe href:http: //www.xyz.com

Now my users see a page in abcd.com where there is an iframe like <iframe src="http://www.xyz.com/" style="width:688px; height:384px;"></iframe>

现在我的用户在 abcd.com 中看到一个页面,其中有一个 iframe,如 <iframe src="http://www.xyz.com/" style="width:688px; height:384px;"></iframe>

Which displays the login screen of the xyz.com

显示 xyz.com 的登录屏幕

Now what i want is, if users login to xyz.com another new tab should open with the homepage, after login of xyz.com and abcd.com tab should get close.

现在我想要的是,如果用户登录 xyz.com,另一个新标签应该打开主页,登录 xyz.com 和 abcd.com 标签后应该关闭。

Is it possible to do with javascript or jquery, which works for all the browser including IE 7,8,9 ??

是否可以使用 javascript 或 jquery,它适用于包括 IE 7、8、9 在内的所有浏览器?

Thanks

谢谢

回答by epascarello

Open up a new tab

打开一个新标签

link

关联

<a href="foo.html" target="_blank">linky</a>

pop up

弹出

window.open("foo.html", "_blank");

回答by AbdelHady

Put the following javascript code in the <head>tag of the homepage of xyz.com (but don't put it in the login page if you still want to keep the login page inside the iframe):

<head>xyz.com主页的标签中加入如下javascript代码(但如果你还想将登录页面保留在iframe中,则不要将其放入登录页面):

<script type="text/javascript">
    if (top.location!= self.location){
        top.location = self.location
    }
</script>

now, when opening the home page of xyz.com, this code will break the <iframe>and changes the current tab from abcd.com to the home page of xyz.com

现在,当打开 xyz.com 的主页时,此代码将破坏<iframe>并将当前选项卡从 abcd.com 更改为 xyz.com 的主页

回答by JDhjps

i dont know what you mean, but opening a new tab from iframe is just the normal code.

我不知道你的意思,但从 iframe 打开一个新标签只是正常的代码。

<a href="WebAdress.netdom" target="_blank"><button>Open New Tab</button></a>

hope this helps

希望这可以帮助

回答by Marcel Aveneil

What you're trying to do is impossible on your end, you only supply a login page inside the iFrame, therefor you cant control what the "login" button actually does, it's controlled by the other website.

您尝试做的事情最终是不可能的,您只在 iFrame 内提供一个登录页面,因此您无法控制“登录”按钮的实际作用,它由其他网站控制。

The other website will need to edit the action themselves and add target='_blank', but that wont happen as their users will have another tab when they login.

另一个网站需要自己编辑操作并添加 target='_blank',但这不会发生,因为他们的用户在登录时会有另一个选项卡。