Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a limitation on an IFRAME containing another IFRAME with the same URL?

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):

frames screenshot

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);
}
like image 345
Motti Avatar asked May 09 '11 08:05

Motti


1 Answers

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);
  };

})();
like image 62
davin Avatar answered Sep 18 '22 15:09

davin