views:

596

answers:

1

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

A: 

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.

brownstone