Safari的“存储访问API”仅适用于iframe吗?

时间:2019-09-10 22:49:46

标签: cookies safari

我试图在模式内部的普通JavaScript中使用Storage Access API。我没有使用iframe。我在网上看到的所有有关Storage Access API参考iframe的文档。这是否意味着该技术仅适用于iframe,还是可以在常规javascript文件中使用它?

我尝试将其附加到html中的onclick事件上,并使用javascript以编程方式创建它,但这些方法似乎都不起作用。无法显示“在浏览“ news.example.com”时是否要允许'video.example'使用Cookie和网站数据”。

<button onlick="showSafariMessage()"  type="button">Show Safari Message</div>

<script>
    var showSafariMessage = function () {
        document.hasStorageAccess().then(hasAccess => {
            if (!hasAccess) {
                return document.requestStorageAccess();
            }
        }).then((result) => {
            // Now we have first-party storage access!

            // Let's access some items from the first-party cookie jar
            document.cookie = "foo=bar"; // set a cookie
            localStorage.setItem("username", "John"); // access a localStorage entry
        }).catch((error) => {
            // error obtaining storage access.
        });
    }
</script>

我希望看到Safari弹出窗口,但事实并非如此。请帮忙!

0 个答案:

没有答案