[ js ] window.open母視窗如何執行子視窗函式?

看板Ajax作者 (不知...)時間13年前 (2011/01/02 08:26), 編輯推噓0(006)
留言6則, 2人參與, 最新討論串1/1
試做了一個Demo http://tinyurl.com/38awpzf 母視窗:test1.html 子視窗:test2.html 點了母視窗頁面上的按鈕後,用window.open開了子視窗 有正確把值傳給了子視窗面裡的textarea 但如果要由母視窗傳變數到子視窗的變數 或是由母視窗執行子視窗的函式該如何做呢? PassForm: 視窗名稱 Agrs : 變數 PassForm.document.getElementById("Text1").value = Agrs; //正確可行 PassForm.document.testValue = Agrs; PassForm.document.testFunction(); //這兩項好像都失敗了 完整JS如下 母視窗: var TestCount = 0; var Form; function PassValueToForm(PassForm, Agrs) { if ( PassForm.document.getElementById("Text1")) { PassForm.document.getElementById("Text1").value = Agrs; PassForm.document.testValue = Agrs; PassForm.document.testFunction(); } else { if (TestCount <= 10) { setTimeout(function() { PassValueToForm(InForm, Agrs); }, 1000); } else { alert("失敗"); } } } //按鈕事件: function Button2_onclick() { TestCount = 0; Form = window.open("test2.html",name,'height=300,width=500'); setTimeout(function() { PassValueToForm(Form, 'toTest2Value'); }, 1000); } 子視窗: var testValue = '20110102'; //測試用的變數,也是希望被改變的變數 //兩個測試的Function function testFunction(){ alert("test2 Say Hi"); } function testFunction2(){ alert(testValue); } 查了一陣子,不太清楚方向... 不知道是不能這樣執行 或是要用其他方法?(例如cookie) 麻煩了... -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.32.165.26 ※ 編輯: Angelliya 來自: 114.32.165.26 (01/02 08:27) ※ 編輯: Angelliya 來自: 114.32.165.26 (01/02 08:27)

01/02 10:36, , 1F
只有一行錯誤 PassForm.document.testFunction(); 改成
01/02 10:36, 1F

01/02 10:37, , 2F
PassForm.testFunction(); function物件都在window物件下
01/02 10:37, 2F

01/02 10:38, , 3F
不在 document下 所以當你去用setTimeout clousure時
01/02 10:38, 3F

01/02 10:39, , 4F
主視窗的那一行出錯了 造成後面的setTimeOut都沒做
01/02 10:39, 4F

01/02 10:52, , 5F
PassForm.document.testValue = Agrs; 也要改
01/02 10:52, 5F

01/02 14:29, , 6F
成功了...非常謝謝回答與註解^^
01/02 14:29, 6F
文章代碼(AID): #1D7yOd9V (Ajax)