GM.getValue: Difference between revisions

From GreaseSpot Wiki
Jump to navigationJump to search
m (Text replace - "[[Metadata_block" to "[[Metadata Block")
(simplify)
Line 3: Line 3:
== Description ==
== Description ==


This [[API_reference|API]] method retrieves a value that was set with [[GM_setValue]]. If nothing was set and a default value is passed, it returns that default. Failing that, it returns undefined.
This method retrieves a value that was set with [[GM_setValue]].
 
See [[GM_setValue]] for details on the storage of these values.
Values are saved in the [http://developer.mozilla.org/en/docs/Code_snippets:Preferences Firefox preferences] back end and can be manually changed by typing [[mozillazine:About:config|about:config]] in the address bar and searching for the preference name "<code>greasemonkey.scriptvals.[[Metadata Block#.40namespace|namespace]]/[[Metadata Block#.40name|name]].foo</code>".
 


Compatibility: [[Version_history#0.3_beta|Greasemonkey 0.3b+]]


== Syntax ==
== Syntax ==


'''GM_getValue(''' ''name'', ''default'' ''')'''
{{Function|GM_getValue|name, default}}


:Value: Function
:Returns: String, Integer, Boolean, undefined and null<sup>[[Talk:GM_getValue|[1]]]</sup>
:Compatibility: [[Version_history#0.3_beta|Greasemonkey 0.3b+]]


:{| cellpadding="5" style="border-style:solid; background:#FFFFE0;"
=== Arguments ===
|+ Parameters
!style="background:#CC9900;"|'''Properties'''
|-
| <code><span style="background:#FFFFE0;">[[#name |name]]</span></code>
|-
|<code><span style="background:#FFFFE0;">[[#default |default]]</span></code>
|}
:* All properties are optional except [[#name|name]].


=== Properties ===
; <code>name</code>
----
: <code>String</code> The property name to get. See [[GM_setValue#Arguments|GM_setValue]] for details.
==== <code>name</code> ====
; <code>default</code>
:Value: String
: Any value to be returned, when no value has previously been set.
:Usage: <code>'''name''' = "PropertyName";</code>


:* Property name to retrieve.
=== Returns ===


; When this <code>name</code> has been set
: <code>String</code>, <code>Integer</code> or <code>Boolean</code> as previously set
; When this <code>name</code> has not been set, and <code>default</code> is provided
: The value passed as <code>default</code>
; When this <code>name</code> has not been set, and <code>default</code> is not
: <code>null</code>


==== <code>default</code> ====
== Examples ==
:Value: String, Integer or Boolean
:Usage: <code>'''default''' = 5;</code>


:* If [[GM_getValue#name|name]] is not found, then use this property to return a [[#default |default]] value. If [[#default |default]] is omitted JavaScript undefined is returned.
: Note: Using this property does not call [[GM_setValue]] and is by design.
== Examples ==
<pre class='sample'>
<pre class='sample'>
GM_log(GM_getValue("foo"));
GM_log(GM_getValue("foo"));
Line 51: Line 36:


== See Also ==
== See Also ==
* [[GM_setValue]]
* [[GM_setValue]]
* [[GM_deleteValue]]
* [[GM_deleteValue]]
* [[GM_listValues]]
* [[GM_listValues]]
== Notes ==


[[Category:API_Reference|G]]
[[Category:API_Reference|G]]

Revision as of 21:35, 8 February 2010


Description

This method retrieves a value that was set with GM_setValue. See GM_setValue for details on the storage of these values.

Compatibility: Greasemonkey 0.3b+

Syntax

function GM_getValue( name, default )


Arguments

name
String The property name to get. See GM_setValue for details.
default
Any value to be returned, when no value has previously been set.

Returns

When this name has been set
String, Integer or Boolean as previously set
When this name has not been set, and default is provided
The value passed as default
When this name has not been set, and default is not
null

Examples

GM_log(GM_getValue("foo"));

See Also