Question

I noticed something peculiar about the "with" keyword in javascript and the parent and child window relationship, specifically window.opener. I haven't tested this from the parent window, just the child, but it is worth noting in the example below--

Parent window (Parent.html):

// global scope
function ParentFunc() { alert("I am a parent function"); }

Child window (Child.html):

// global scope
var baseWin = window.opener;

// "this" keyword corresponds to Child.html when function below is called
function Child_Onclick_Func()
{
  alert("Hi, from Child");
  with (baseWin)
  {
    ParentFunc();
    alert("Hi, from Parent");
  }
  alert("Hi, back to Child");
}

The "with" keyword, in this case, switches to the parent window and the second alert fires an implicit onfocus to the parent window, as well. I didn't realize "with" would switch to the parent window, but it makes sense now.

Was it helpful?

Solution

This happens because window is the global namespace when running javascript in a web browser. When you write:

alert('Hello, World!');

You are actually calling the window.alert method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top