简体   繁体   English

如何检测浏览器是否支持window.close?

[英]How to detect whether a browser supports window.close?

I'm building an app, where the auth flow goes as follows 我正在构建一个应用,身份验证流程如下

  • user clicks login button 用户单击登录按钮
  • a login window opens. 将打开一个登录窗口。
  • The user is redirected to either Facebook, Google or Twitter, depending on the querystring of the window. 根据窗口的查询字符串,将用户重定向到Facebook,Google或Twitter。
  • When the user comes back from fb/google/twitter, my backend generates an auth token, and the popup window uses postMessage to send the auth token to the main page, verifying the origin 当用户从fb / google / twitter返回时,我的后端会生成一个auth令牌,然后弹出窗口使用postMessage将auth令牌发送到主页,以验证来源
  • the main page closes th window after receiving the token. 收到令牌后,主页将关闭窗口。

This flow works great on desktop devices, but not in mobile browsers, where window.open opens a new tab that can't be programmatically closed. 此流程在台式机设备上效果很好,但在移动浏览器中效果不佳,因为window.open会打开无法以编程方式关闭的新标签页。

On mobile devices, I can redirect the user to the login screen, and send them back with the auth token in the hash fragment (oauth2 implicit flow), but I only want to do that when I have to. 在移动设备上,我可以将用户重定向到登录屏幕,并使用哈希片段中的auth令牌(oauth2隐式流)将他们发送回去,但我只想在必须这样做时这样做。 How do I detect whether window.close works without browser sniffing? 如何检测window.close是否在没有浏览器嗅探的情况下工作?

What I've tried 我尝试过的

I've tried using a HTML/JS based modal dialog with an iframe in it, but Google's login page doesn't allow embedding it in an iFrame, I haven't tried with other providers 我尝试使用基于HTML / JS的模式对话框以及iframe,但是Google的登录页面不允许将其嵌入iFrame,也没有尝试与其他提供程序一起使用

You can check if window.close() is supported by object detection: 您可以检查对象检测是否支持window.close()

if (window.close) {
    alert ('window close is supported');
}

Do the check without paranthesis () , as you don't want to execute the function, but want to check for existance of the function/object. 进行不带paranthesis ()的检查,因为您不想执行该功能,但是想要检查该功能/对象的存在。

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

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