views:

102

answers:

1

Hello, Im making firefox extension. One function stores value in every tab, to use it later by other function.

function setValue(value) {

   var attr = gBrowser.document.createAttribute("value");

   attr.value = value;

   gBrowser.document.attributes.setNamedItem(attr);

};



function getValue() {

  var attr = gBrowser.document.attributes.getNamedItem("value");

  if (attr != undefined && attr != null)

    return attr.value;

  else

    return null;

};

For some reason, this doesn't work. Can you spot an error in my code?
Function getValue() should get value of active tab.

A: 

There are more errors than code here:

  • There's no gBrowser.document - you probably meant gBrowser.ownerDocument or just document (equivalent but simpler).
  • Neither is gBrowser.document.attributes, you meant gBrowser.attributes.
  • Using attributes seems very weird, an equivalent to the fixed up version of your code would be gBrowser.setAttribute("value", value) and gBrowser.getAttribute("value")
  • The fixed up code still isn't what you probably need and you didn't specify what exactly do you need (gBrowser.mCurrentTab.setAttribute?)
Nickolay
Instead of using attributes, you can also just use Javascript properties, because in Javascript (unlike, say, Java) you're always free to just attach new properties to an object: `gBrowser.value = 'someValue'` (assuming that 'value' doesn't already have some meaning -- you'd have to check.)
MatrixFrog