重新打开应用程序时,iOS 12中的PWA不再重新执行Javascript

我有一个PWA,它在使用Javascript打开时实际上将用户重定向到消息应用程序。随着iOS 12的推出和PWA的更改,网页在重新打开或重新获得焦点时不再重新初始化并执行Javascript。相反,它现在加载先前保存的状态,并且不会重新执行Javascript。


有人对此有任何想法吗?每当PWA关注时,我可以强制执行Javascript吗?我可以强制页面在加载时重新初始化吗?


下面的示例代码:


<html manifest="ios/scripts/offline.manifest">


<head>

<meta content="en-us" http-equiv="Content-Language" />

<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />

<title>SMS</title>


<meta name="viewport" content="width=device-width, initial-scale=1">

<meta name="apple-mobile-web-app-title" content="SMS">

<meta name="apple-mobile-web-app-capable" content="yes" />

<meta name="apple-mobile-web-app-status-bar-style" content="black" />



<link rel="apple-touch-icon" sizes="180x180" href="ios/img/Icon-60x60@3x.png">

<link rel="apple-touch-startup-image" href="ios/img/LaunchImage-1125@3x~iphoneX-portrait_1125x2436.png" media="(device-width: 375px) and (device-height: 812px) and (-webkit-device-pixel-ratio: 3) and (orientation: portrait)">

</head>


<body>

<script>

        if (window.navigator.standalone) {

            document.write('<a id="url" href="sms:1111111111" name="url"></a>');

            var e = document.getElementById('url');

            var ev = document.createEvent('MouseEvents');

            ev.initEvent('click', true, true);

            e.dispatchEvent(ev);

        }

</script>

</body>


</html>


蓝山帝景
浏览 158回答 1
1回答

拉风的咖菲猫

我遇到了同样的问题。这是我强迫PWA在开始时执行javascript的操作:在标记之间嵌入的javascript中注册页面可见性事件。在页面可见性事件的事件处理程序中,执行您希望在随后打开PWA时执行的javascript。每当打开PWA时,它将触发页面可见性事件并执行您的脚本。示例:在您的html文件中:<script>registerPageVisibility()</script>function registerPageVisibility() {&nbsp; &nbsp;&nbsp; &nbsp; let hidden;&nbsp; &nbsp; let visibilityChange;&nbsp; &nbsp; if (typeof document.hidden !== 'undefined') { // Opera 12.10 and Firefox 18 and later support&nbsp; &nbsp; &nbsp; &nbsp; hidden = 'hidden';&nbsp; &nbsp; &nbsp; &nbsp; visibilityChange = 'visibilitychange';&nbsp; &nbsp; } else if (typeof document.msHidden !== 'undefined') {&nbsp; &nbsp; &nbsp; &nbsp; hidden = 'msHidden';&nbsp; &nbsp; &nbsp; &nbsp; visibilityChange = 'msvisibilitychange';&nbsp; &nbsp; } else if (typeof document.webkitHidden !== 'undefined') {&nbsp; &nbsp; &nbsp; &nbsp; hidden = 'webkitHidden';&nbsp; &nbsp; &nbsp; &nbsp; visibilityChange = 'webkitvisibilitychange';&nbsp; &nbsp; }&nbsp; &nbsp; window.document.addEventListener(visibilityChange, () => {&nbsp; &nbsp; &nbsp; &nbsp; if (!document[hidden]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //put your script here and it will be execute everytime when PWA is opened.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript