Solution 1 :

The short and simplified answer is yes if the set URL differs in more than the fragment from them previous one.

The long and complex answer is given in the current specification (which, by the way, 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.

The steps for processing the iframe attributes state:

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.

The steps to navigate an iframe or frame say this:

Navigate element’s nested browsing context to resource…

And finally, navigating a browsing context defines this:

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.

At that point the browsing context is changed to the new resource.

Note that the quoted parts of the specification only reflect the cases of fragments and changing the URL to something else than a “javascript” or fetch scheme URL.

Problem :

According HTML5 official specs, they state:

When an iframe element is removed from a document, the user agent must discard the nested browsing context, if any

So I wonder if the browsing context is also discarded if I change the src or srcDoc attributes.

Specs also mention about it:

When the user agent is to process the iframe attributes, it must run the first appropriate steps from the following list

But it is not clear to me if setting/changing this attributes will discard the current browsing context. If not, will the JavaScript execution context be discarded?

By