Question

Say you have an iframe and you are modifying its contentDocument with javascript.

Theoretically you can do this by either setting the contentDocument.body.innerHTML directly or using contentDocument.write()

See this example on jsbin

In chrome both work just fine. In firefox however the top one flashes in and out. (In IE10 the top one throws an error with the body being null but I'm not worried about IE).

Two questions:

  • Why does this happen in firefox?
  • Which behavior is the "correct" one according to the spec?
Was it helpful?

Solution

In Firefox, when you create an iframe it starts loading about:blank. Then your script races with that load: you modify the DOM, and then about:blank loads and wipes out your changes.

"The spec" doesn't define the behavior here so far (like most of document loading), though there's a proposal in webapps that would do so. That proposal would be that there should be no about:blank load in this situation and instead a fake load event should get fired even though no load happened (because websites depend on frames with no src firing load events).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top