Cordova - iframe 中的 localStorage 在应用程序重启时被清除
Cordova - localStorage in iframe gets cleared on application restart
index.html 文件当前如下所示:
<!DOCTYPE html><html><head style="overflow: hidden; margin:0; padding:0;border:0;">
<meta charset='UTF-8'/>
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src * 'unsafe-inline' 'unsafe-eval'" />
</head>
<body style="overflow: hidden; margin:0; padding:0; width:100vw; height:100vh;border:0;">
<iframe src="http://domain.com" style="overflow: hidden; margin:0; padding:0; width:100vw; height:100vh;border:0;"></iframe>
</body>
<script type=text/javascript>
alert(window.localStorage.getItem("aaa"));
window.localStorage.setItem("aaa", "111")
</script>
</html>
脚本中的警报 会在应用程序 重新启动 时持续存在 。
无论 iframe 使用 localStorage,在应用程序重新启动时被清除,使得 iframe 的使用不等于伪意图。
您应用的 localStorage 不会与远程域共享,因为 localStorage 仅特定于域,因此您需要使用不同的技术来传递任何共享数据。这里有一些方法:
将值作为查询字符串参数传递给 iframe URL。您始终可以将 iframe 附加到 DOM 等,或者如果移动到 SPA,则首先在其他地方预加载 localStorage 值。
使用更常见的技术,让您的父页面向 iframe 发送 postMessage
并向 iframe 添加代码以侦听 message
事件。
有关如何send/recieve postMessage 的更多详细信息:
https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage
index.html 文件当前如下所示:
<!DOCTYPE html><html><head style="overflow: hidden; margin:0; padding:0;border:0;">
<meta charset='UTF-8'/>
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src * 'unsafe-inline' 'unsafe-eval'" />
</head>
<body style="overflow: hidden; margin:0; padding:0; width:100vw; height:100vh;border:0;">
<iframe src="http://domain.com" style="overflow: hidden; margin:0; padding:0; width:100vw; height:100vh;border:0;"></iframe>
</body>
<script type=text/javascript>
alert(window.localStorage.getItem("aaa"));
window.localStorage.setItem("aaa", "111")
</script>
</html>
脚本中的警报 会在应用程序 重新启动 时持续存在 。
无论 iframe 使用 localStorage,在应用程序重新启动时被清除,使得 iframe 的使用不等于伪意图。
您应用的 localStorage 不会与远程域共享,因为 localStorage 仅特定于域,因此您需要使用不同的技术来传递任何共享数据。这里有一些方法:
将值作为查询字符串参数传递给 iframe URL。您始终可以将 iframe 附加到 DOM 等,或者如果移动到 SPA,则首先在其他地方预加载 localStorage 值。
使用更常见的技术,让您的父页面向 iframe 发送
postMessage
并向 iframe 添加代码以侦听message
事件。
有关如何send/recieve postMessage 的更多详细信息:
https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage