Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xml/xul Access to dtd variable via javascript

I would like to create a xul/xml element dynamically via javascript such as :

var toolbarbutton = document.createElement("toolbarbutton");
toolbarbutton.setAttribute("tooltiptext", "&variable;");

"variable" is set in my dtd file but I can't access to it with these code. I know that if I had created the element directly in my xul/xml file, I would have been able to write something like :

<toolbarbutton tooltiptext="&variable;">

Writing it dynamically, it doesn't work. It just puts "&variable;". Do you know why ?

like image 940
user2302725 Avatar asked Dec 19 '25 22:12

user2302725


1 Answers

DTDs will be considered only when the XML document is parsed.

A valid work-around would be to simply put the DTD "variables" somewhere in the XML (and therefore DOM) where they don't bother and then read them as needed.

<someelement id="someelement"
  data-myaddon-active="&tooltip.active;"
  data-myaddon-inactive="&tooltip.inactive;"></overlay>

(data-* isn't processed by anything, other than your add-on)

var someelement = document.getElementById("someelement");
var toolbarbutton = document.createElement("toolbarbutton");
toolbarbutton.setAttribute("tooltiptext", somelement.getAttribute("data-myaddon-active");

This hack is used throughout the regular mozilla code base as well, e.g. browser.xul has:

<window id="main-window"
        ...
        title="&mainWindow.title;"
        title_normal="&mainWindow.title;"
        title_privatebrowsing="&mainWindow.title;&mainWindow.titlemodifiermenuseparator;&mainWindow.titlePrivateBrowsingSuffix;"
        ...>

And then uses something along the lines of the following at runtime:

if (private)
  document.title = mainWindow.getAttribute("title_privatebrowsing");
like image 114
nmaier Avatar answered Dec 21 '25 12:12

nmaier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!