使用 javascript 关闭移动浏览器选项卡

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10924284/
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-10-26 11:29:19  来源:igfitidea点击:

Closing a mobile browser tab with javascript

javascriptjqueryandroidiosjquery-mobile

提问by Manuel Martinez

I need a native app to fire a browser with some URL that will take the user to a mobile website. Inside the mobile website, there has to be a button that closes the browser (or sends any signal to the native app) so that the user gets back to the native app. Currently I'm trying to close the window, but I don't think that's gonna do the trick in all mobile devices.

我需要一个本机应用程序来启动带有一些 URL 的浏览器,这些 URL 会将用户带到移动网站。在移动网站内部,必须有一个按钮来关闭浏览器(或向本机应用程序发送任何信号),以便用户返回到本机应用程序。目前我正在尝试关闭窗口,但我认为这不会在所有移动设备中起作用。

My code:

我的代码:

$( document ).bind('pageinit', function(){
   $.mobile.activePage.find('#close').click(function(){
      window.open('', '_self', ''); 
      window.close();
   });
});

I'm using jQuery mobile.

我正在使用 jQuery 移动版。

采纳答案by kabuko

Setup a custom URI handler (for Androidand for iOS). Then all you have to do is redirect to a URL that matches, perhaps using window.location.

设置自定义 URI 处理程序(适用于 AndroidiOS)。然后你所要做的就是重定向到一个匹配的 URL,也许使用window.location.

回答by Ethan Reesor

It seems that there are security restrictions that wouldn't allow you to close the window via JavaScript. See here

似乎存在不允许您通过 JavaScript 关闭窗口的安全限制。看这里

EDIT: You basically have two options: implement a custom URL handler for each platform you're developing for; or embedding a web view into your application (UIWebView for iOS or WebView for Android).

编辑:您基本上有两个选择:为您正在开发的每个平台实现自定义 URL 处理程序;或将 Web 视图嵌入到您的应用程序中(适用于 iOS 的 UIWebView 或适用于 Android 的 WebView)。

回答by Gruntcakes

On iOS if you launch Safari from your app you won't be able to get back to your app after Safari closes, unless your app is registered as a custom URL handler and the page you are on launches a URL that launches your app.

在 iOS 上,如果您从您的应用程序启动 Safari,您将无法在 Safari 关闭后返回您的应用程序,除非您的应用程序注册为自定义 URL 处理程序并且您所在的页面启动了一个启动您的应用程序的 URL。

On iOS if instead of launching Safari you show the web page in a UIWebView you have control over exiting the page.

在 iOS 上,如果不是启动 Safari,而是在 UIWebView 中显示网页,则您可以控制退出页面。