功能使用JavaScript检测在新窗口/标签(target = _blank)中的打开 [英] Feature detect opening in a new window/tab(target=_blank) with JavaScript

查看:432
本文介绍了功能使用JavaScript检测在新窗口/标签(target = _blank)中的打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的研究


  • WebView可以禁用在新窗口/标签页中打开链接。

  • 本机应用程序开发人员使用WebView在其应用程序中显示网页(请参阅Twitter的应用程序)。

  • 通过用户代理检测WebView不起作用

  • 只需尝试使用JS打开新窗口,就会触发弹出窗口阻止程序;
  • 因此,这是测试是否可以打开新窗口的不可靠方法。
  • "WebView" can disable "opening links in new windows/tabs".
  • WebView is used by native app developers to display webpages within their app(see Twitter's app).
  • Detecting WebView via user agent doesn't work consistently and isn't a best practice anyway.
  • Simply attempting to open a new window with JS triggers popup blockers; making it an unreliable way to test if a new window can be opened.

我需要检测该功能何时不可用。

I need to detect when this feature is not available. Impossible?

其他说明

我试图检测是否可以通过target = _blank打开一个新窗口。例如,UIWebView [应用内浏览器]可以防止target = _blank正常工作[它只是在同一窗口中打开,而不是在新窗口中打开]。我需要一个解决方案来指出由于浏览器的限制(例如在UIWebView情况下)何时无法打开新窗口。不幸的是,弹出窗口阻止程序阻止检查这种功能,因为它们在不允许用户输入(即单击)的情况下,永远不允许打开新窗口。

I'm trying to detect whether I can open a new window via target=_blank. For example, UIWebView [in-app browser] can prevent target=_blank from working as expected [it simply opens in the same window instead of a new one]. I need a solution to indicate when a new window can't be opened because of browser limitations such as in the UIWebView case. Unfortunately popup blockers prevent checking such functionality because they never allow a new window to be opened without user input(ie. a click) to be bypassed.

推荐答案

您将无法可靠地达到100%。

You will not have something reliable to 100%.


仅尝试使用JS打开新窗口会触发弹出窗口阻止程序;

Simply attempting to open a new window with JS triggers popup blockers; making it an unreliable way to test if a new window can be opened.

您是正确的,这绝对不可靠... window.open()被阻止(即使有类似 window.open(url,'_blank');之类的技巧; window.focus(); ), click()也被阻止(在包含target = _ blank的链接上),就像 evt = document。 createEvent( MouseEvents); evt.initEvent( click,true,true); ...

You are right it is absolutely not reliable ... window.open() is blocked (even with a trick like window.open(url, '_blank');window.focus();), click() is also blocked (on a link containing target="_blank"), just as evt = document.createEvent("MouseEvents");evt.initEvent("click", true, true);...

但是无论如何:如果WebView不允许在新选项卡中打开链接,这样就可以正常工作。但是正如您所暗示的,Webview可能会对其进行授权。在这种情况下,您将不知道自己是否在Web视图中。很容易检测新标签页中的打开链接是否最终以相同的方式打开(可以在未显示的iframe中的javascript中进行测试),但是如果该链接是在浏览器中打开的,您将无法知道(并想像一下使用JavaScript代码从应用程序在浏览器中打开新标签页的用户体验...)。正如Dave Alperovich所说,如果不尝试,您将无法事先知道将阻止什么。因此,您不应该站在那一边。

But anyway : if the WebView does not allow opening a link in a new tab, then it will work ok. But as you are implying a Webview may authorize it. In this case you will not know if you are in a webview or not. It is easy to detect whether an open link in new tab finally opened in the same or not (can be tested in javascript in a non-displayed iframe), but if the link is opened in a browser, you have no way of knowing (and imagine the user experience with a javascript code that opens a new tab in the browser from an application...). As Dave Alperovich said, you can not know in advance what will be blocked or not, without having tried. So you should not look on that side.

没有可靠的功能或行为将Webview与Web浏览器区分开来。在Web视图中,您将拥有浏览器中的所有内容(Cookie,WebStorage ...)。用户代理有其缺陷,但在许多情况下都可以使用。 此处此处进行构建。

There are no reliable features or behavior that differentiates a Webview from a web browser. In a webview you have all what you get in a browser (cookies, WebStorage ...). User agent has its imperfections but it will works in many cases. There are explanations here or here to build it.

这篇关于功能使用JavaScript检测在新窗口/标签(target = _blank)中的打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆