2011-10-29 72 views
1

我有:我想了解Firefox擴展,所以我下載了包含來自http://kb.mozillazine.org/Getting_started_with_extension_development的「Hello World」示例的zip文件。用於Firefox擴展的Javascript變量

在hello.xul我:

<hbox align="center"> 
<description flex="1">text in box</description> 
</hbox> 

(使文本 「文本框」 彈出框)

在overlay.js中我有:

var HelloWorld = { 
    onLoad: function() { 
    // initialization code 
    this.initialized = true; 
    }, 

    onMenuItemCommand: function() { 
    window.open("chrome://helloworld/content/hello.xul", "", "chrome"); 
    var a = "text I want in box"; 
    } 
}; 

window.addEventListener("load", function(e) { HelloWorld.onLoad(e); }, false); 

問題:如何在JavaScript文件中使用變量a,以便該變量的內容是「打印」在框中的內容?

回答

1

首先,請仔細閱讀本:https://developer.mozilla.org/en/DOM/window.openDialog#Passing_extra_parameters_to_the_dialog

您應該使用參數從一個窗口傳遞值到另一個窗口(Dialog Concept)

這提供了一個簡單的方法來跨越xul文件傳遞值。

對於你的問題,你可以在xxx.xul中做這樣的事情。這將打開hello.xul與額外的參數returnValues沿:

var returnValues = { out: null }; 
window.openDialog("hello.xul", "tree", "modal", returnValues); 

注模是必須的。

接下來你xxx.xul,店內所有的值(可以稱之爲Y),你想傳遞給hello.xul如下:

window.arguments[0].out = y 

window.argument[0]指returnValues

現在,您可以訪問y的值(這是你的情況標籤的名稱)在hello.xul如下:

var labels = returnValues.out; 

基本盟友,

您打開它時,將參數傳遞給子窗口。

然後在子窗口中填入希望傳遞迴父窗口的值並關閉子窗口的參數。

現在回到父窗口,您可以訪問傳遞給子窗口的參數,並且它包含由子窗口更新的信息。

2

您需要將參數傳遞到新窗口。一個例子顯示here