views:

196

answers:

1

My firefox extension have to:

  1. Save an address of current page,
  2. Open new page
  3. Put this address into content of the new page, if a button will be pressed.

Problem is, how to save to var this address, becouse if I will do it from first function, the function that is from button doesnt have this var. Should i use a global function, or something?

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;
}

Function openTab() is execute, when proper button is pressed, the same with write() function

+1  A: 
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 And this example? everything is encapsulated in 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>
andres descalzo
Sorry, but i cannot write <pre>var path = null;</pre> outside any function. Functions openTab() and write() are execute, when proper button is pressed.
I have thought that it wont work, but it works! thank you :)
ohh, it works too much :( Now when i press openTab button from page that have got "xxx" address, and then press openTab button in "yyy" page, 2 new pages that poped up have got the same this.paht!