[Project_owners] Getting document from dialog close onunload

HJ bugus at universum.org
Sat Feb 26 04:19:55 EST 2005


Bradley Rintoul wrote:
> HJ wrote:
> 
>> Bradley Rintoul wrote:
>>
>>> Bradley Rintoul wrote:
>>>
>>>> After a dialog box is closed, I'm trying to take a pref set by the 
>>>> dialog and change a toolbar's textbox background color accordingly.  
>>>> I have a function set as a handler for the "onunload" event, but I 
>>>> can't figure out how to get ahold of the toolbar's textbox...(the 
>>>> toolbar lives in the browser)  I put document.getElementById in the 
>>>> onunload handler, but I think it's still referring to the dialog's 
>>>> DOM...  How do I get ahold of the "browers" DOM document so that I 
>>>> can get the textbox in question...?
>>>>
>>>> Thanks,
>>>> B. Rintoul
>>>>
>>>>
>>> I figured out how to do this - thought I'd share it in case some 
>>> don't know how to do this kind of thing...
>>>
>>> I did this:
>>>    var element = opener.document.getElementById( "textbox-id" );
>>>
>>> and then called "setAttribute" for the style on the returned element...
>>
>>
>>
>> That's nice, but what if you like to change the color for this toolbar 
>> in all windows?
>>
>> Getting the service:
>> var observerService = 
>> Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService); 
>>
>>
>> Adding the observer:
>> observerService.addObserver(myTextBoxObserver, 
>> "textbox-color-changed", false);
>>
>> The observer:
>> var myTextBoxObserver = {
>>   observe: function(subject, topic, state) {
>>     if (topic == "textbox-color-changed") {
>>      // do some work
>>     }
>>   }
>> }
>>
>> Notifying the observer:
>> observerService.notifyObservers(null, "textbox-color-changed", aColor);
>>
>> Removing the observer:
>> observerService.removeObserver(myTextBoxObserver, 
>> "textbox-color-changed");
>>
>> But you can also fire a custom made event and use a simple 
>> eventlistener, do it the way you think is necessary/appropriate.
>>
>> /HJ
>>
> Great info - thanks.
> 
> How do I initialize the color of my textbox when the browser is first 
> launched so that it has the correct background color (based on a 
> preference already set)?  I've tried to add some script outside of a 
> funtion in my overlay that adds an event listener to the window "load" 
> event but the function I'm trying to call doesn't seem to get called...

You could add a few lines to you overlay, for example:

var myListener = {
   init: function()
   {
     var color = pref.getIntPref(...);
     var observerService =
Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService); 

     observerService.notifyObservers(null, "textbox-color-changed", color);
   }
};
addEventListener("load", myListener.init, false);

/HJ


More information about the Project_owners mailing list