Monday, February 21, 2011

How to load javascript in iframe as a part of header and us it for child frames

Loading javascript in iframe like gmail loading javascript in hidden iframe(js_frame) and make it context sensitive.

From stackoverflow
  • To access the JavaScript of a parent frame you simply use the parent keyword. For example:

    child.htm:

    <script>
      var childVariable = 'child';
    
      function showParentVariable() {
        alert('from child: '+parent.parentVariable);
      }
    
      window.onload = function() {
        if (!!parent.child && parent.child===window) {
          parent.showChildVariable();
        }
      }
    </script>
    

    parent.htm:

    <iframe src="child.htm"></iframe>
    <script>
      var parentVariable = 'parent';
      var child = window.frames[0];
    
      function showChildVariable() {
        alert('from parent: '+child.childVariable);
      }
    
      window.onload = function() {
        child.showParentVariable();
      }
    </script>
    

    Here is a live demo: Child Parent

    I don't know what you mean by "context sensitive" though.

0 comments:

Post a Comment