Javascript 如何在 iphone 的独立(全屏)模式下打开一个新窗口

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

How to open a new window on iphone's standalone (fullscreen) mode

javascriptweb-applicationsmobile-safariiphoneiphone-standalone-web-app

提问by Eduardo Abreu

<!doctype html>
<html>
<head>
<title>page</title>
<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0"/>
<meta name="apple-mobile-web-app-capable" content="yes" />

<meta name="apple-mobile-web-app-status-bar-style" content="black" />
</head>
<body>
<script>
function goToPage() {
    var pageUrl = 'http://www.google.com/';
    window.open(pageUrl);
}
</script>
<div id="installBtn" onclick="goToPage()">go to page</div>
</body>
</html>

The expected action is: when touch the div, a new window opens. This code works great in the iPhone's safari.

预期的动作是:当触摸 div 时,会打开一个新窗口。这段代码在 iPhone 的 safari 中效果很好。

But when I tap "+" -> "Add to Home Screen", and press "go to page", no window is opened, and the page loads in the same screen.

但是当我点击“+”->“添加到主屏幕”,然后按“转到页面”时,没有窗口打开,页面在同一屏幕中加载。

How to force, by javascript, a new window to open in the standalone mode?

如何通过 javascript强制在独立模式下打开一个新窗口?

回答by JonWarnerNet

The below question does mention a possible JavaScript solution out of the top voted answers, which constructs the anchor element and dispatches the 'click' event on it.

下面的问题确实提到了一个可能的 JavaScript 解决方案,它构建了锚元素并在其上调度 'click' 事件。

Question: Force link to open in mobile safari from a web app with javascript
Answer: https://stackoverflow.com/a/8833025/1441046

问题:使用 javascript 从 Web 应用程序强制在移动 Safari 中打开链接
答案:https: //stackoverflow.com/a/8833025/1441046

Alternatively if you can use an anchor element (I know you asked how to do it in JavaScript) you can do the following:

或者,如果您可以使用锚元素(我知道您问过如何在 JavaScript 中使用),您可以执行以下操作:

<a id="installBtn" href="http://www.google.com/" target="_blank">go to page</a>

Other related questions:

其他相关问题:

iPhone window.open(url, '_blank') does not open links in mobile Safari

iPhone window.open(url, '_blank') 无法在移动版 Safari 中打开链接

回答by Matt

This works for me. Doesn't work when requesting it from html, only from JS.

这对我有用。从 html 请求它时不起作用,只能从 JS 请求。

window.open('[url]','_system');

回答by Musman

you can use childbrowser to open in the standalone mode

您可以使用子浏览器在独立模式下打开

Or you can use this

或者你可以使用这个

window.location = url(your Url);

回答by Remi Grumeau

There you go! (if you still need it)

你去吧!(如果你还需要的话)

<script>
    if(window.navigator.standalone === true) 
     document.write('Standalone');
    else
     document.write('Web browser');
</script>

R.

R。