【问题标题】:Cordova - localStorage in iframe gets cleared on application restartCordova - iframe 中的 localStorage 在应用程序重新启动时被清除
【发布时间】:2016-12-29 08:46:51
【问题描述】:

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 的使用不等于伪意图。

【问题讨论】:

    标签: cordova web local-storage phonegap-plugins cordova-plugins


    【解决方案1】:

    您应用的 localStorage 不会与远程域共享,因为 localStorage 仅特定于域,因此您需要使用不同的技术来传递任何共享数据。以下是几种方法:

    1. 将该值作为查询字符串参数传递给 iframe URL。如果移动到 SPA,您始终可以将 iframe 附加到 DOM 等,或者先在其他地方预加载 localStorage 值。

    2. 使用更常见的技术,让您的父页面向 iframe 发送 postMessage,并向 iframe 添加代码以侦听 message 事件。

    有关如何发送/接收 postMessage 的其他详细信息:

    https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-19
      • 2017-10-23
      • 1970-01-01
      • 2014-09-10
      • 2022-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多