In order to play around a bit with frame hierarchy I wrote a small html page which allows nesting an arbitrary number of frames (code available at the end of the question).
However this doesn't work, on IE9 and Firefox 4 the inner frames are created but aren't rendered (the head
and body
are empty):
and on Chrome it works for two levels and then if I click the Add
button on the inner frame nothing happens (no error message in the console either).
If I copy the file N
times and have each file use a different file it works for any depth (but not if there's a cycle).
I tried to search for such a limitation but I must not have used the right keywords. Does anyone have any reference for this?
Here's the addRemoveFrames.html file:
<!DOCTYPE html>
<html>
<head>
<title>Add and Remove Frames</title>
<script type="text/javascript">
function add() {
var f = document.createElement('iframe');
f.src = 'addRemoveFrames.html';
document.getElementById('frameContainer').appendChild(f);
}
function remove() {
var c = document.getElementById('frameContainer');
var f = c.lastChild;
if (f)
c.removeChild(f);
}
</script>
</head>
<body>
<input type="button" onclick="add()" value="Add"/>
<input type="button" onclick="remove()" value="Remove"/>
<hr />
<div id="frameContainer"></div>
</body>
</html>
I've modified @davin's answer slightly so each frame's URL reflects its full path in the hierarchy.
var counter = 0;
function add() {
var f = document.createElement('iframe');
var sep = location.search ? (location.search + '.') : "?";
f.src = 'addRemoveFrames.html' + sep + ++counter;
document.getElementById('frameContainer').appendChild(f);
}
Looks like a sensible browser security mechanism to prevent an infinite loop of nested iframes (even though in your case it wouldn't be infinite).
In any case, a simple workaround could be to add a useless query parameter to the url, making the browser think the page loaded isn't identical, but really it is.
So instead of your current function add()
, something like this (I went all out so id
doesn't polute the global namespace):
var add = (function(){
var id = 0;
return function(){
var f = document.createElement('iframe');
f.src = 'addRemoveFrames.html?useless=' + id++;
document.getElementById('frameContainer').appendChild(f);
};
})();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With