When does a web browser request the src content of a dynamically created iframe?
dom, html, iframe, javascript
Solution
The specification states:
When an `iframe` element is first inserted into a document, the user agent must create a nested browsing context, and then process the `iframe` attributes for the first time.
Using the following snippet and a local server, I've tested the behaviour in many browsers.
var f = document.createElement('iframe');
f.src = '/?';
The resource is never fetched (I've only shown the lowest and highest tested browser version):
- IE 6 - 9
- FF 3.6 - 12.0
- Chrome 1 - 18
- Opera 10.00 - 12.00
- Safari 4.0 - 5.1.5
So, it the request is only sent once the frame is appended to the document.
Problem
Let's say I dynamically create an iframe node by using js, then set the src attribute (1) and finally append the node to the DOM (2). When is the src content requested by the browser? After (1)? After (2)? Or is it unpredictable? Does it depend on the browser?