How to load javascript in an iframe as part of the header, and we load it for child frames

Load javascript in an iframe like gmail, load javascript in a hidden iframe (js_frame) and make it context sensitive.

0


a source to share


1 answer


To access the JavaScript of the parent frame, you simply use the parent keyword . For instance:

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".

0


a source







All Articles