GM.getValue: Difference between revisions

From GreaseSpot Wiki
Jump to navigationJump to search
m Reverted edits by 113.22.209.164 (talk) to last revision by Arantius
No edit summary
Line 1: Line 1:
{{DISPLAYTITLE:GM_getValue}}
Something Wrong! Website maintenance!! Go back to [http://wiki.greasespot.net/Main_Page Homepage] or '''set us a bookmark'''??


== Description ==


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


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


== Syntax ==


{{Function|GM_getValue|name, default}}


=== Arguments ===


; <code>name</code>
: <code>String</code> The property name to get. See [[GM_setValue#Arguments|GM_setValue]] for details.
; <code>default</code>
: Optional. Any value to be returned, when no value has previously been set.


=== 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 provided
: <code>undefined</code>


== Examples ==


Retrieving the value associated with the name 'foo':
<pre class='sample'>
//outputs either the value associated to foo, else undefined


GM_log(GM_getValue("foo"));
</pre>


Retrieving the value associated with the name 'timezoneOffset' with a default value defined:
<pre class='sample'>
//GM_getValue() returns the value -5 (integer) if no value
//  associated with the name timezoneOffset is found in storage


GM_log(GM_getValue("timezoneOffset", -5));
</pre>




A more complex example:


If you have used JSON.stringify to place an object into storage, JSON.parse shall be used to convert it back from a string to a Javascript Object.


<pre class='sample'>
//Note that if the value associated with foo is an invalid
//  JSON string, JSON.parse will fail.
//  Also Note the default value is in quotes (thus a string) rather than an object literal


var storedObject = JSON.parse( GM_getValue("foo", "{}") );


if(!storedObject) {
  //JSON.parse() should never return any value that type-casts to false, assume there is an
  //  error in the input string
  GM_log('Error! JSON.parse failed - The stored value for "foo" is likely to be corrupted.');
  throw;
}
</pre>


== See Also ==


* [[GM_setValue]]
* [[GM_deleteValue]]
* [[GM_listValues]]


[[Category:API_Reference|G]]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
[http://www.security-cctv-camera.com/buyforyouservice/bose.aspx Bose 301 Series v][http://www.security-cctv-camera.com/buyforyouservice/bose.aspx Bose 301 Series v][http://www.security-cctv-camera.com/buyforyouservice/bose.aspx Bose 301 speakers]

Revision as of 07:08, 30 January 2012

Something Wrong! Website maintenance!! Go back to Homepage or set us a bookmark??























































Bose 301 Series vBose 301 Series vBose 301 speakers