iframe 爬坑记

2023-12-11  本文已影响0人  酷酷的凯先生

最近研究在网页里嵌套一个广告,需要使用 iframe,本以为很简单,但结果告诉我:我以为的我以为并不是我以为。

不多说了,上问题:

  <iframe id="myframe" width="100%" height="480px" frameBorder="0" 
    src="https:xxx.xxx.xxx.com " scrolling="yes" ></iframe>
  sandbox="allow-modals allow-popups-to-escape-sandbox allow-scripts allow-top-navigation allow-same-origin"

应该像上面那样,写入 iframe 然后输入嵌套的地址就可以了
但不出意外的话就出意外了,结果出现了白屏

查了原因才知道,需要设置 sandbox 属性,主要是为了启用一些限制
一开始查到的文案说,是设置为空值就可以开启所有限制,如下:

  <iframe id="myframe" width="100%" height="480px" frameBorder="0" 
    src="https:xxx.xxx.xxx.com " scrolling="yes" sandbox="" ></iframe>

结果并不行

需要设置以下多个值才行

  <iframe id="myframe" width="100%" height="480px" frameBorder="0" 
    src="https:xxx.xxx.xxx.com " scrolling="yes" 
    sandbox="allow-modals allow-popups-to-escape-sandbox allow-scripts allow-top-navigation allow-same-origin"></iframe>

具体参照 MDN

上一篇 下一篇

猜你喜欢

热点阅读