我有一个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>
答案 0 :(得分:1)
我遇到了同样的问题。这是我强迫PWA在开始时执行javascript的操作:
每次打开PWA都会触发页面可见性事件并执行脚本。
示例:在您的html文件中:
<script>
registerPageVisibility()
</script>
function registerPageVisibility() {
let hidden;
let visibilityChange;
if (typeof document.hidden !== 'undefined') { // Opera 12.10 and Firefox 18 and later support
hidden = 'hidden';
visibilityChange = 'visibilitychange';
} else if (typeof document.msHidden !== 'undefined') {
hidden = 'msHidden';
visibilityChange = 'msvisibilitychange';
} else if (typeof document.webkitHidden !== 'undefined') {
hidden = 'webkitHidden';
visibilityChange = 'webkitvisibilitychange';
}
window.document.addEventListener(visibilityChange, () => {
if (!document[hidden]) {
//put your script here and it will be execute everytime when PWA is opened.
}
});
}
答案 1 :(得分:0)
看看这篇关于PWA'state'的帖子:Progressive Web App Progress in iOS 12.2 Beta 1 (Build 16E5181f)