When a tab-switching event

I was looking for event that can run some code after clicking (changing) a tab in the browser, but I don't know, t found this.

Is there any way to achieve this event?

I am already listening to the DOMContentLoaded event, I am showing some information about the current page in the status bar. When I changed the tab, the information in the status bar is incorrect (I need to reload the page).

Thank you

+4
source share
1 answer

There are two possible ways to do this.

Location change detection

The nsIWebProgressListener interface is used to determine when the window location has changed. Put something like this in the onLoad window handler:

 // implements nsIWebProgressListener var listener = { onLocationChange: function(aProgress, aRequest, aURI) { // fires on tab change; update status here; // get the currently selected tab var currDoc = gBrowser.contentDocument; }, onStateChange: function(a, b, c, d) {}, onProgressChange: function(a, b, c, d, e, f) {}, onStatusChange: function(a, b, c, d) {}, onSecurityChange: function(a, b, c) {} } gBrowser.addProgressListener( listener, Components.interfaces.nsIWebProgress.NOTIFY_LOCATION); 

Please note that onLocationChange triggered every time the window location is changed. This means that it starts when the browser starts, when a new URL is loaded into the current tab, when the tab changes, etc. This is probably what you want if your goal is to update the status bar based on the current URL load.

Detecting Tab Changes

To determine only the case when a new tab was selected, use the TabSelect event. The full example is copied here:

 function exampleTabSelected(event) { var browser = gBrowser.selectedBrowser; // browser is the XUL element of the browser that just been selected } // During initialisation var container = gBrowser.tabContainer; container.addEventListener("TabSelect", exampleTabSelected, false); // When no longer needed container.removeEventListener("TabSelect", exampleTabSelected, false); 
+5
source

All Articles