簡體   English   中英

如何在尚不支持的瀏覽器中處理 iframe 的“允許模式”沙箱標志?

[英]How to handle the 'allow-modals' sandbox flag for iframes in browsers that do not yet support it?

從 Chrome 46 開始,需要將 'allow-modals' 標志添加到 iframe 的沙箱屬性中,以允許模式(如警報和確認)脫離 iframe。 到目前為止沒有問題。

但是,當您在尚不支持該標志的瀏覽器(如版本 46 之前的 Safari 或 Chrome)中運行該代碼時,您會收到以下錯誤:解析“沙箱”屬性時出錯:“允許模態”是無效的沙箱標志。

有人知道如何在沒有某種瀏覽器嗅探的情況下解決這個問題嗎?

似乎唯一的方法是通過 JS 追溯添加它。

 function allowModals(){ for (const i of document.getElementsByTagName('iframe')) { if (!i.sandbox.supports('allow-modals')) { console.warn("Your browser doesn't support the 'allow-modals' attribute :("); break; } if (i.sandbox.contains('allow-modals')) continue; console.info(i, "doesn't allow modals"); i.sandbox.add('allow-modals'); console.info(i, 'now allows modals'); } }
 <button onclick='allowModals()' style='display: block'>Allow modals</button> <iframe src="//example.com"></iframe>

新的屬性值不能在舊瀏覽器中使用,這似乎很奇怪。 向后不兼容的更改不是網絡應該工作的方式。 :/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM