1 <h1>Background Pages
</h1>
4 <p id=
"eventPageWarning" class=
"warning">
5 <em>Caution:
</em> Consider using event pages instead.
6 <a href=
"event_pages">Learn more
</a>.
10 A common need for extensions is to have
11 a single long-running script to manage some task or state.
12 Background pages to the rescue.
16 As the
<a href=
"overview#arch">architecture overview
</a> explains,
17 the background page is an HTML page that runs in the extension process.
18 It exists for the lifetime of your extension,
19 and only one instance of it at a time is active. (Exception: if your
20 extension uses
<a href=
"manifest/incognito">incognito
</a>
21 "split" mode, a second instance is created for incognito windows.)
25 In a typical extension with a background page,
27 for example, the browser action or page action
28 and any options page
—
29 is implemented by dumb views.
30 When the view needs some state,
31 it requests the state from the background page.
32 When the background page notices a state change,
33 the background page tells the views to update.
36 <h2 id=
"manifest">Manifest
</h2>
39 Register your background page in the
40 <a href=
"manifest">extension manifest
</a>.
41 In the common case, a background page
42 does not require any HTML markup.
43 These kind of background pages can be
44 implemented using JavaScript files alone,
48 <pre data-filename=
"manifest.json">
50 "name":
"My extension",
53 "scripts": [
"background.js"]
59 A background page will be generated
60 by the extension system
61 that includes each of the files listed
62 in the
<code>scripts
</code> property.
66 If you need to specify HTML
67 in your background page, you can
68 do that using the
<code>page
</code>
72 <pre data-filename=
"manifest.json">
74 "name":
"My extension",
77 "page":
"background.html"
83 If you need the browser to start up early
—so
84 you can display notifications, for example
—then
85 you might also want to specify the
86 <a href=
"declare_permissions#background">"background" permission
</a>.
90 <h2 id=
"details">Details
</h2>
93 You can communicate between your various pages using direct script calls,
94 similar to how frames can communicate.
95 The $(ref:extension.getViews) method
96 returns a list of window objects
97 for every active page belonging to your extension,
99 $(ref:extension.getBackgroundPage) method
100 returns the background page.
103 <h2 id=
"example">Example
</h2>
106 The following code snippet demonstrates
107 how the background page
108 can interact with other pages in the extension.
109 It also shows how you can use
110 the background page to handle events
115 The extension in this example
116 has a background page
117 and multiple pages created
120 from a file named
<code>image.html
</code>.
124 <pre data-filename=
"background.js">
125 // React when a browser action's icon is clicked.
126 chrome.browserAction.onClicked.addListener(function(tab) {
127 var viewTabUrl = chrome.extension.getURL('image.html');
128 var imageUrl =
<em>/* an image's URL */
</em>;
130 // Look through all the pages in this extension to find one we can use.
131 var views = chrome.extension.getViews();
132 for (var i =
0; i < views.length; i++) {
135 // If this view has the right URL and hasn't been used yet...
136 if (view.location.href == viewTabUrl && !view.imageAlreadySet) {
138 // ...call one of its functions and set a property.
139 view.setImageUrl(imageUrl);
140 view.imageAlreadySet = true;
146 <pre data-filename=
"image.html">
149 function setImageUrl(url) {
150 document.getElementById('target').src = url;
159 <img
id=
"target" src=
"white.png" width=
"640" height=
"480">