Services.jsm

  • Revision slug: JavaScript_code_modules/Services.jsm
  • Revision title: Services.jsm
  • Revision id: 37229
  • Created:
  • Creator: Neil
  • Is current revision? No
  • Comment 73 words added, 3 words removed

Revision Content

{{ gecko_minversion_header("1.9.3") }}

The Services.jsm JavaScript code module offers a wide assortment of lazy getters that simplify the process of obtaining references to commonly used services.

To use it, you first need to import the code module into your JavaScript scope:

Components.utils.import("resource://gre/modules/Services.jsm");

Then you can obtain references to services by simply accessing them from the Services object exported by the code module. For example, to obtain a reference to the preferences service:

var prefsService = Services.prefs;

Provided service getters

Service Accessor Service Interface Service Name
appinfo {{ interface("nsIXULAppInfo") }}
{{ interface("nsIXULRuntime") }}
Application information service
console {{ interface("nsIConsoleService") }} Error console service
dirsvc {{ interface("nsIDirectoryService") }}
{{ interface("nsIProperties") }}
Directory service
droppedLinkHandler {{ interface("nsIDroppedLinkHandler") }} Dropped link handler service
io {{ interface("nsIIOService") }}
{{ interface("nsIIOService2") }}
I/O Service
locale {{ interface("nsILocaleService") }} Locale service
obs {{ interface("nsIObserverService") }} Observer service
perms {{ interface("nsIPermissionManager") }} Permission manager service
prefs {{ interface("nsIPrefBranch") }}
{{ interface("nsIPrefBranch2") }}
{{ interface("nsIPrefService") }}
Preferences service
prompt {{ interface("nsIPromptService") }} Prompt service
scriptloader {{ interface("mozIJSSubScriptLoader") }} JavaScript subscript loader service
search {{ interface("nsIBrowserSearchService") }} Browser search service¹
storage {{ interface("mozIStorageService") }} Storage API service
strings {{ interface("nsIStringBundleService") }} String bundle service
tm {{ interface("nsIThreadManager") }} Thread Manager service
vc {{ interface("nsIVersionComparator") }} Version comparator service
wm {{ interface("nsIWindowMediator") }} Window mediator service
ww {{ interface("nsIWindowWatcher") }} Window watcher service

¹Not present in Thunderbird or SeaMonkey.

See also

Revision Source

<p>{{ gecko_minversion_header("1.9.3") }}</p>
<p>The <code>Services.jsm</code> JavaScript code module offers a wide assortment of lazy getters that simplify the process of obtaining references to commonly used services.</p>
<p>To use it, you first need to import the code module into your JavaScript scope:</p>
<pre class="eval"><span class="nowiki">Components.utils.import("resource://gre/modules/Services.jsm");</span></pre>
<p>Then you can obtain references to services by simply accessing them from the <code>Services</code> object exported by the code module. For example, to obtain a reference to the preferences service:</p>
<pre class="brush: js">var prefsService = Services.prefs;
</pre>
<h2>Provided service getters</h2>
<table class="standard-table" style="width: auto;"> <tbody> <tr> <td class="header">Service Accessor</td> <td class="header">Service Interface</td> <td class="header">Service Name</td> </tr> <tr> <td><code>appinfo</code></td> <td>{{ interface("nsIXULAppInfo") }}<br> {{ interface("nsIXULRuntime") }}</td> <td>Application information service</td> </tr> <tr> <td><code>console</code></td> <td>{{ interface("nsIConsoleService") }}</td> <td>Error console service</td> </tr> <tr> <td><code>dirsvc</code></td> <td>{{ interface("nsIDirectoryService") }}<br> {{ interface("nsIProperties") }}</td> <td>Directory service</td> </tr> <tr> <td><code>droppedLinkHandler</code></td> <td>{{ interface("nsIDroppedLinkHandler") }}</td> <td>Dropped link handler service</td> </tr> <tr> <td><code>io</code></td> <td>{{ interface("nsIIOService") }}<br> {{ interface("nsIIOService2") }}</td> <td>I/O Service</td> </tr> <tr> <td><code>locale</code></td> <td>{{ interface("nsILocaleService") }}</td> <td>Locale service</td> </tr> <tr> <td><code>obs</code></td> <td>{{ interface("nsIObserverService") }}</td> <td>Observer service</td> </tr> <tr> <td><code>perms</code></td> <td>{{ interface("nsIPermissionManager") }}</td> <td>Permission manager service</td> </tr> <tr> <td><code>prefs</code></td> <td>{{ interface("nsIPrefBranch") }}<br> {{ interface("nsIPrefBranch2") }}<br> {{ interface("nsIPrefService") }}</td> <td>Preferences service</td> </tr> <tr> <td><code>prompt</code></td> <td>{{ interface("nsIPromptService") }}</td> <td>Prompt service</td> </tr> <tr> <td><code>scriptloader</code></td> <td>{{ interface("mozIJSSubScriptLoader") }}</td> <td>JavaScript subscript loader service</td> </tr> <tr> <td><code>search</code></td> <td>{{ interface("nsIBrowserSearchService") }}</td> <td>Browser search service¹</td> </tr> <tr> <td><code>storage</code></td> <td>{{ interface("mozIStorageService") }}</td> <td><a href="/en/Storage" title="en/Storage">Storage API</a> service</td> </tr> <tr> <td><code>strings</code></td> <td>{{ interface("nsIStringBundleService") }}</td> <td>String bundle service</td> </tr> <tr> <td><code>tm</code></td> <td>{{ interface("nsIThreadManager") }}</td> <td><a href="/en/The_Thread_Manager" title="en/The Thread Manager">Thread Manager</a> service</td> </tr> <tr> <td><code>vc</code></td> <td>{{ interface("nsIVersionComparator") }}</td> <td>Version comparator service</td> </tr> <tr> <td><code>wm</code></td> <td>{{ interface("nsIWindowMediator") }}</td> <td>Window mediator service</td> </tr> <tr> <td><code>ww</code></td> <td>{{ interface("nsIWindowWatcher") }}</td> <td>Window watcher service</td> </tr> </tbody>
</table>
<p>¹Not present in Thunderbird or SeaMonkey.</p><h2>See also</h2>
<ul> <li><a class="internal" href="/en/JavaScript_code_modules/Using_JavaScript_code_modules" title="en/JavaScript code modules/Using
    JavaScript code
    modules">Using JavaScript code modules</a></li> <li><a class="internal" href="/en/JavaScript_code_modules" title="en/JavaScript code
    modules">JavaScript code modules</a></li> <li><a class="internal" href="/en/XPCOM/mozilla::services_namespace" title="mozilla::services C++ namespace">mozilla::services C++ namespace</a></li>
</ul>
Revert to this revision