1

javascriptの「with」キーワードと親子ウィンドウの関係、特にwindow.openerに特有の何かに気づきました。私はこれを親ウィンドウからテストしていません。子だけですが、以下の例では注目に値します-

親ウィンドウ(Parent.html):

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

子ウィンドウ(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");
}

この場合、「with」キーワードは親ウィンドウに切り替わり、2番目のアラートは親ウィンドウにも暗黙のオンフォーカスを発生させます。「with」が親ウィンドウに切り替わるとは思いもしませんでしたが、今では理にかなっています。

4

1 に答える 1

1

これwindowは、Webブラウザでjavascriptを実行するときのグローバル名前空間が原因で発生します。あなたが書くとき:

alert('Hello, World!');

You are actually calling the window.alert method.

于 2011-06-16T15:33:01.077 に答える