2012-03-09 22 views
0

我有以下的JavaScript代碼 -如何使用Javascript將一個窗口中的變量的值發送到新窗口?

function addOnClickEventHandler() 
    { 
     var userNameElement = document.getElementById("currentUser"); 
     var userName = userNameElement.value; // send this value to "chatWindow" 

     window.open("chatWindow.html", "Chat Window", "resizable=0,width=700,height=600"); 
    } 

現在,我怎麼username的值發送到 「chatWindow」?另外,一旦它被髮送,如何訪問這個值?

回答

1

在打開的窗口中,您可以參考父窗口,如下所示:

var oWinCaller = top.opener;

通過此參考,您可以在打開新窗口時從打開的窗口中獲取所需的任何內容。

+0

因此,例如。 oWinCaller.userName讓我的用戶名?但新窗口是HTML。 HTML會將其視爲純文本。如何解決這個問題? – CodeBlue 2012-03-09 15:31:14

+0

您可以放置​​一個元素,例如新窗口html中的隱藏字段,並將父項的值寫入其中。 – 5arx 2012-03-09 15:35:26

+0

@ 5arx請問這樣做? CodeBlue 2012-03-09 15:41:04

0

你可以嘗試

window.open("chatWindow.html?id=1", "Chat Window", "resizable=0,width=700,height=600"); 

To Read Argument create js function in opening page and create following function 

function getArgs() { 
var args = new Object(); 
var query = location.search.substring(1); 
var pairs = query.split("&"); 
for (var i = 0; i < pairs.length; i++) { 
    var pos = pairs[i].indexOf('='); 
    if (pos == -1) continue; 
    var argname = pairs[i].substring(0, pos); 
    var value = pairs[i].substring(pos + 1); 
    args[argname] = unescape(value); 
} 
return args; 

} 然後用 變參= getArgs();
var hid = args.id;

+0

然後,如何在窗口內訪問它? – CodeBlue 2012-03-09 15:24:46

+0

@CodeBlue我編輯了我的答案,將做。 – 2012-03-09 15:30:24

+0

先生,我很抱歉,但對我來說這太複雜了! – CodeBlue 2012-03-09 15:41:31

相關問題