• Skip to main content
  • Select language
  • Skip to search
MDN Web Docs
  • Technologies
    • HTML
    • CSS
    • JavaScript
    • Graphics
    • HTTP
    • APIs / DOM
    • WebExtensions
    • MathML
  • References & Guides
    • Learn web development
    • Tutorials
    • References
    • Developer Guides
    • Accessibility
    • Game development
    • ...more docs
Add-ons
  1. MDN
  2. Mozilla
  3. Add-ons
  4. Legacy extensions for Firefox for Android
  5. API
  6. Prompt.jsm

Prompt.jsm

In This Article
  1. Basic usage
  2. Method overview
  3. Methods
    1. addButton()
    2. addCheckbox()
    3. addTextbox()
    4. addPassword()
    5. addMenulist()
    6. show()
    7. setSingleChoiceItems()
    8. setMultiChoiceItems()

Add-ons using the techniques described in this document are considered a legacy technology in Firefox. Don't use these techniques to develop new add-ons. Use WebExtensions instead. If you maintain an add-on which uses the techniques described here, consider migrating it to use WebExtensions.

Starting from Firefox 53, no new legacy add-ons will be accepted on addons.mozilla.org (AMO) for desktop Firefox and Firefox for Android.

Starting from Firefox 57, WebExtensions will be the only supported extension type. Desktop Firefox and Firefox for Android will not load other extension types.

Even before Firefox 57, changes coming up in the Firefox platform will break many legacy extensions. These changes include multiprocess Firefox (e10s), sandboxing, and multiple content processes. Legacy extensions that are affected by these changes should migrate to WebExtensions if they can. See the "Compatibility Milestones" document for more information.

A wiki page containing resources, migration paths, office hours, and more, is available to help developers transition to the new technologies.

Note: Prompt.jsm is still under development. The API may change. Use with care!

The Prompt.jsm JavaScript code module offers utility routines dealing with showing dialogs and prompts in Fennec. The API is chainable, so most methods return the prompt object to you. To use it, you first need to import the code module into your JavaScript scope:

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

Basic usage

Prompt.jsm provides a Prompt constructor for the current scope. The constructor takes a single Object as an argument. That object can have properties for the prompts:

Parameter Description
window The window that is opening this prompt. Optional
title The title to show on the prompt. Optional
message A message to show on the prompt. Optional
buttons An array of Strings to show as buttons on the prompt. Prompts on Android support a maximum of three buttons. Any more will be ignored. Optional

Example:

var p = new Prompt({
  window: window,
  title: "My title",
  message: "This is the text on my prompt",
  buttons: ["OK", "Cancel"]
}).show(function(data) {
  alert("Clicked on: " + data.button);
});

Method overview

addButton(aOptions)
addCheckbox(aOptions)
addTextbox(aOptions)
addPassword(aOptions)
addMenulist(aOptions)
show(aCallback)
setSingleChoiceItems(aItems)
setMultiChoiceItems(aItems)

Methods

addButton()

Adds a button to the prompt. A maximum of three buttons can be shown at the dialog at any time. If there are already three buttons present, this will silently fail.

Prompt addButton(aOptions);
Parameters

Takes an object with parameters describing the button

Parameter Description
label The label to show on the button.
Return value

Returns the Prompt that is being modified.

Example
var p = new Prompt({
  window: window,
  title: "My Prompt",
  message: "A message on the prompt"
});
if (shouldShowButtons) {
  p.addButton({ label: "Button 1" });
  p.addButton({ label: "Button 2" });
}
p.show(function(data) {
  if (data.button == 0)
    alert("Clicked button 1!");
  else if (data.button == 1)
    alert("Clicked button 2!");
});

addCheckbox()

Adds a checkbox to the prompt.

Prompt addCheckbox(aOptions);
Parameters

Takes an object with parameters describing the checkbox.

Parameter Description
label The label to show next to the checkbox.
checked A boolean describing whether or not the checkbox is checked.
id An id to be associated with the checkbox. This can be used to identify the value of the checkbox in the return value. If not specified, the checkbox will be associated with an id of "checkbox" plus the index of this checkbox. i.e. "checkbox0", "checkbox1", etc.
Return value

Returns the Prompt that is being modified.

Example
var p = new Prompt({
  window: window,
  title: "My Prompt",
  message: "A message on the prompt"
}).addCheckbox({
  label: "My checkbox",
  checked: true,
  id: "myCheckBox"
}).show(function(data) {
  alert("Checkbox is " + (data.myCheckBox ? "" : "NOT") + " checked.");
});

addTextbox()

Adds a textbox to the prompt.

Prompt addTextbox(aOptions);
Parameters

Takes an object with parameters describing the textbox.

Parameter Description
autofocus A boolean indicating if this textbox should focus itself when the dialog is shown.
value The default value of the textbox
id An id to be associated with the textbox. This can be used to identify the value of the textbox in the return value. If not specified, the textbox will be associated with an id of "textbox" plus the index of this textbox. i.e. "textbox0", "textbox1", etc.
hint Some hint text to show inside the textbox if its empty.
Return value

Returns the Prompt that is being modified.

Example
var p = new Prompt({
  window: window,
  title: "Enter a username",
}).addTextbox({
  value: "Jim Brown",
  id: "username",
  hint: "User name",
  autofocus: true
}).show(function(data) {
  alert("Username is " + data.username);
});

addPassword()

Adds a password textbox to the prompt.

Prompt addPassword(aOptions);
Parameters

Takes an object with parameters describing the textbox.

Parameter Description
autofocus A boolean indicating if this textbox should focus itself when the dialog is shown. If multiple elements on the dialog try to autofocus, the behavior is undefined.
value The default value of the textbox
id An id to be associated with the textbox. This can be used to identify the value of the textbox in the return value. If not specified, the textbox will be associated with an id of "textbox" plus the index of this textbox. i.e. "textbox0", "textbox1", etc.
hint Some hint text to show inside the textbox if its empty.
Return value

Returns the Prompt that is being modified.

Example
var p = new Prompt({
  window: window,
  title: "Enter a username",
}).addTextbox({
  value: "Jim Brown",
  id: "username",
  hint: "User name",
  autofocus: true
}).addPassword({
  value: "JimsPassword",
  id: "password",
  hint: "Password",
}).show(function(data) {
  alert(data.username + "'s password is " + data.password);
});

addMenulist()

Adds a menulist (a.k.a. an Android spinner) to the prompt.

Prompt addMenulist(aOptions);
Parameters

Takes an object with parameters describing the menulist.

Parameter Description
values An array of strings to show in the menulist
id An id to be associated with the menulist. This can be used to identify the value of the textbox in the callback. If not specified, the menulist will be associated with an id of "menulist" plus an idex. i.e. "menulist0", "menulist1", etc.
Return value

Returns the Prompt that is being modified.

Example
var colors = ["Red", "Green", "Blue"];
var p = new Prompt({
  window: window,
  title: "Whats your favorite color?",
}).addMenulist({ values: colors }).show(function(data) {
  alert("Your favorite color is: " + colors[data.menulist0]);
});

show()

Shows the prompt

show(aCallback);
Parameters

Takes a callback function to be called when the prompt returns. The callback function is passed an object containing the values of returned inputs, as well as a button parameter with the index of whichever button or list item was clicked. For the setMultiChoiceItems method, the object has a list property consisting of the selected items.

Return value

None

Example
var p = new Prompt({
  window: window,
  title: "Everything ok?",
  buttons: ["Ok", "Not ok"]
}).show(function(data) {
  if(data.button == 1)
    alert(":(");
  else
    alert(":)");
});

setSingleChoiceItems()

Adds a set of single-choice list items to the prompt. This will show items as a scrollable list inside the dialog, like a context menu, or a HTML select would appear.

Prompt setSingleChoiceItems(aItems);
Parameters

Takes an array of objects with parameters describing the list items. Objects can have the following parameters:

Parameter Description
label The label to show on this list item
selected Whether or not this item is selected. Adding selected to any item will cause all of the items in the list to have an indicator on their right to describe if they're selected or not.
disabled Whether or not this item is disabled.
children Used if this is a header row. Children is an array of menuitems to be shown underneith the header and are indented to indicate they belong inside it.
submenu Used if this row contains a submenu. An indicator will be shown on the right to indicate this item has a submenu. Showing the submenu when this item is selected is up to the implementation.
Return value

Returns the Prompt that is being modified.

Example
var items = [
  { label: "Shades of red" },
  { label: "Pink", disabled: true, child: true },
  { label: "Maroon", child: true },
  { label: "Green", selected: true },
  { label: "Blue" },
];
var p = new Prompt({
  window: window,
  title: "Whats your favorite color?",
}).setSingleChoiceItems(items).show(function(data) {
  alert("Your favorite color is: " + items[data.button]);
});

setMultiChoiceItems()

Adds a set of multi-choice items to the list. This is very similar to setSingleChoiceItems above, but the list will not dismiss when an item is clicked.

Prompt setMultiChoiceItems(aItems);
Parameters

Takes an array of objects with parameters describing the list items. Objects can have the following parameters:

Parameter Description
label The label to show on this list item
selected Whether or not this item is selected. Adding selected to any item will cause all of the items in the list to have an indicator on their right to describe if they're selected or not.
disabled Whether or not this item is disabled.
children Used if this is a header row. Children is an array of menuitems to be shown underneith the header and are indented to indicate they belong inside it.
submenu Used if this row contains a submenu. An indicator will be shown on the right to indicate this item has a submenu. Showing the submenu when this item is selected is up to the implementation.
Return value

Returns the Prompt that is being modified.

Example
var items = [
  { label: "Shades of red", }
  { label: "Pink", disabled: true, child: true },
  { label: "Maroon", child: true },
  { label: "Green", selected: true },
  { label: "Blue", selected: true },
];
var p = new Prompt({
  window: window,
  title: "What colors do you like?",
  // Without one or more buttons, at least Firefox 34 will close
  // the menu after selecting one option.
  buttons: [ "OK" ],
}).setMultiChoiceItems(items).show(function(data) {
  if (!data.list) {
    alert("Cancelled!");
    return;
  }
  var colors = data.list.map(function(i) {
    return items[i].label;
  });
  alert("Your favorite colors are " + colors.join(","));
});

Document Tags and Contributors

 Contributors to this page: rebloor, andrewtruongmoz, wbamberg, Lekensteyn, backy0175, leibovic, wesj
 Last updated by: rebloor, Jun 19, 2017, 7:43:38 PM
See also
  1. WebExtensions
  2. Getting started
    1. What are WebExtensions?
    2. Your first WebExtension
    3. Your second WebExtension
    4. Anatomy of a WebExtension
    5. Example WebExtensions
  3. How to
    1. Intercept HTTP requests
    2. Modify a web page
    3. Add a button to the toolbar
    4. Implement a settings page
  4. User interface
    1. Introduction
    2. Toolbar button
    3. Address bar button
    4. Sidebar
    5. Context menu items
    6. Options page
    7. Bundled web pages
    8. Notifications
    9. Address bar suggestions
    10. Developer tools panels
  5. Concepts
    1. Using the JavaScript APIs
    2. Content scripts
    3. Match patterns
    4. Internationalization
    5. Content Security Policy
    6. Native messaging
  6. Porting
    1. Porting a Google Chrome extension
    2. Porting a legacy Firefox add-on
    3. Embedded WebExtensions
    4. Comparison with the Add-on SDK
    5. Comparison with XUL/XPCOM extensions
    6. Chrome incompatibilities
    7. Differences between desktop and Android
  7. Firefox workflow
    1. Temporary Installation in Firefox
    2. Debugging
    3. Developing for Firefox for Android
    4. Getting started with web-ext
    5. web-ext command reference
    6. WebExtensions and the Add-on ID
    7. Publishing your WebExtension
  8. JavaScript APIs
    1. Browser support for JavaScript APIs
    2. alarms
    3. bookmarks
    4. browserAction
    5. browsingData
    6. commands
    7. contextMenus
    8. contextualIdentities
    9. cookies
    10. devtools.inspectedWindow
    11. devtools.network
    12. devtools.panels
    13. downloads
    14. events
    15. extension
    16. extensionTypes
    17. history
    18. i18n
    19. identity
    20. idle
    21. management
    22. notifications
    23. omnibox
    24. pageAction
    25. permissions
    26. privacy
    27. proxy
    28. runtime
    29. sessions
    30. sidebarAction
    31. storage
    32. tabs
    33. topSites
    34. types
    35. webNavigation
    36. webRequest
    37. windows
  9. Manifest keys
    1. applications
    2. author
    3. background
    4. browser_action
    5. chrome_settings_overrides
    6. chrome_url_overrides
    7. commands
    8. content_scripts
    9. content_security_policy
    10. default_locale
    11. description
    12. developer
    13. devtools_page
    14. homepage_url
    15. icons
    16. incognito
    17. manifest_version
    18. name
    19. omnibox
    20. optional_permissions
    21. options_ui
    22. page_action
    23. permissions
    24. protocol_handlers
    25. short_name
    26. sidebar_action
    27. version
    28. web_accessible_resources
  10. Add-on SDK
  11. Getting started
    1. Installation
    2. Getting started
    3. Troubleshooting
  12. High-Level APIs
    1. addon-page
    2. base64
    3. clipboard
    4. context-menu
    5. hotkeys
    6. indexed-db
    7. l10n
    8. notifications
    9. page-mod
    10. page-worker
    11. panel
    12. passwords
    13. private-browsing
    14. querystring
    15. request
    16. selection
    17. self
    18. simple-prefs
    19. simple-storage
    20. system
    21. tabs
    22. timers
    23. ui
    24. url
    25. webextension
    26. widget
    27. windows
  13. Low-Level APIs
    1. /loader
    2. chrome
    3. console/plain-text
    4. console/traceback
    5. content/content
    6. content/loader
    7. content/mod
    8. content/symbiont
    9. content/worker
    10. core/heritage
    11. core/namespace
    12. core/promise
    13. dev/panel
    14. event/core
    15. event/target
    16. frame/hidden-frame
    17. frame/utils
    18. fs/path
    19. io/byte-streams
    20. io/file
    21. io/text-streams
    22. lang/functional
    23. lang/type
    24. loader/cuddlefish
    25. loader/sandbox
    26. net/url
    27. net/xhr
    28. places/bookmarks
    29. places/favicon
    30. places/history
    31. platform/xpcom
    32. preferences/event-target
    33. preferences/service
    34. remote/child
    35. remote/parent
    36. stylesheet/style
    37. stylesheet/utils
    38. system/child_process
    39. system/environment
    40. system/events
    41. system/runtime
    42. system/unload
    43. system/xul-app
    44. tabs/utils
    45. test/assert
    46. test/harness
    47. test/httpd
    48. test/runner
    49. test/utils
    50. ui/button/action
    51. ui/button/toggle
    52. ui/frame
    53. ui/id
    54. ui/sidebar
    55. ui/toolbar
    56. util/array
    57. util/collection
    58. util/deprecate
    59. util/list
    60. util/match-pattern
    61. util/object
    62. util/uuid
    63. window/utils
  14. Firefox for Android
  15. Getting started
    1. Walkthrough
    2. Debugging
    3. Code snippets
  16. APIs
    1. Accounts.jsm
    2. BrowserApp
    3. HelperApps.jsm
    4. Home.jsm
    5. HomeProvider.jsm
    6. NativeWindow
    7. Notifications.jsm
    8. PageActions.jsm
    9. Prompt.jsm
    10. RuntimePermissions.jsm
    11. Snackbars.jsm
    12. Sound.jsm
    13. Tab
  17. Legacy
  18. Restartless extensions
    1. Overview
  19. Overlay extensions
    1. Overview
  20. Themes
  21. Publishing add-ons
  22. Guides
    1. Signing and distribution overview
    2. Submit an add-on
    3. Creating an appealing listing
    4. Review policies
    5. Developer agreement
    6. Featured add-ons
    7. Contact addons.mozilla.org
  23. Community and support
  24. Channels
    1. Add-ons blog
    2. Add-on forums
    3. Stack Overflow
    4. Development newsgroup
    5. IRC Channel