简体   繁体   中英

Connecting javascript functions to XUL file

I have a javascript file connected to my XUL file as follows:

<script type="application/javascript"
  src="chrome://myexample/content/myexample.js"/>

The overlay from the XUL file is displayed in Firefox, but my functions aren't working.

eg

<statusbar id="status-bar"> 
<statusbarpanel id="f1"  
    label="f1"
    onclick = "MyExample.f1()"
  />
</statusbar>

myexample.js file looks like:

var MyExample = {
  f1: function() {
  },
  f2: function() {
  }
}

This is my chrome.manifest :

content   myexample  content/
overlay chrome://browser/content/browser.xul  chrome://myexample/content/myexample.xul

Where could be the fault?

There doesn't seem to be anything wrong with the code you posted, apart from the missing = in

var MyExample = {

...not sure if that's a typo in the original code or just in the snippet here.

Did you set the javascript.options.showInConsole and check the Error Console? Are there any messages there when you open the window you try to modify?

You could also be hitting the fact that the chrome code is cached. The effect is that the code you have in your file is not the same code that's running in Firefox. The way to deal with it is to set the disable_xul_cache pref mentioned on the same page as the showInConsole pref I linked to above (and/or run with -purgecaches param). If you have any doubts, make an observable change (ie one that has to change the observed behaviour, eg pops an alert) to the file you think is cached.

[edit] also you could try opening chrome://myexample/content/myexample.js in a tab to see if the chrome.manifest magic is working correctly and you got the URL right, but I guess in your case it's fine.

You dont need MyExample. part.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM