我的Firefox扩展程序必须执行以下操作:
问题是如何将此地址保存到变量。如果地址通过第一页上的函数保存在变量中,则新页面上的功能(必须在按下按钮时显示地址)不具有此变量。我应该使用全局函数吗?
function openTab() {
//Save an address of current page
path=content.location.href;
//Open new Tab and select it
var tab=gBrowser.addTab("chrome://intabeditor/content/editor.html");
var newTabBrowser = gBrowser.getBrowserForTab(tab);
gBrowser.selectedTab=tab;
}
function write() {
content.body.innerHTML=path;
}
按下正确的按钮时,执行函数openTab()
。与write()
函数相同。
答案 0 :(得分:1)
var path = null;
function openTab() {
//Save an address of current page
path=content.location.href;
//Open new Tab and select it
var tab=gBrowser.addTab("chrome://intabeditor/content/editor.html");
var newTabBrowser = gBrowser.getBrowserForTab(tab);
gBrowser.selectedTab=tab;
}
function write() {
if (path!=null)
content.body.innerHTML=path;
}
--- EDIT 这个例子呢?一切都封装在xopen中:
<script type="text/javascript">
var xopen = function()
{
this.path = null;
this.openTab = function()
{
//Save an address of current page
this.path=content.location.href;
//Open new Tab and select it
var tab=gBrowser.addTab("chrome://intabeditor/content/editor.html");
var newTabBrowser = gBrowser.getBrowserForTab(tab);
gBrowser.selectedTab=tab;
}
this.write = function ()
{
if (this.path!=null)
content.body.innerHTML=this.path;
}
}
var wopen = new xopen();
</script>
<button onclick="wopen.openTab()">openTab</button>
<button onclick="wopen.write()">write</button>