根据 HTML5 官方规范,他们声明:
When an iframe element is removed from a document, the user agent must discard the nested browsing context, if any
所以我想知道如果我更改 src 或 srcDoc 属性,浏览上下文是否也会被丢弃。
规范也提到了它:
When the user agent is to process the iframe attributes, it must run the first appropriate steps from the following list
但我不清楚设置/更改此属性是否会丢弃当前浏览上下文。如果不是,JavaScript 执行上下文是否会被丢弃?
最佳答案
如果设置的 URL 与前一个 URL 的片段不同,那么简短的回答是肯定的。
又长又复杂answer is given in the current specification (顺便说一句,is now the "HTML living standard" of the WHATWG):
Whenever an iframe element with a non-null nested browsing context has its srcdoc attribute set, changed, or removed, the user agent must process the iframe attributes.
Similarly, whenever an iframe element with a non-null nested browsing context but with no srcdoc attribute specified has its src attribute set, changed, or removed, the user agent must process the iframe attributes.
steps for processing the iframe attributes状态:
If element's srcdoc attribute is specified, then: … Navigate to the srcdoc resource: navigate an iframe or frame given element … if element has a src attribute specified, or initialInsertion is false, then run the shared attribute processing steps for iframe and frame elements given element.
steps to navigate an iframe or frame这么说:
Navigate element's nested browsing context to resource…
最后,navigating a browsing context定义这个:
If historyHandling is not "reload", resource is a request, resource's url equals browsingContext's active document's URL with exclude fragments flag set, and resource's url's fragment is non-null, then:
Navigate to a fragment given browsingContext
… Otherwise, resource is a request whose url's scheme is neither "javascript" nor a fetch scheme
Run process a navigate URL scheme given resource's url and browsingContext.
此时浏览上下文更改为新资源。
请注意,规范中引用的部分仅反射(reflect)片段和将 URL 更改为“javascript”或获取方案 URL 以外的其他内容的情况。
关于html - 更改或设置 src 或 srcDoc 属性时是否丢弃了 iframe 的 Web 浏览上下文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60254716/