47

Is there a way to retrieve the names/values of all global variables on a page?

I would like to write a javascript function to do the following:

  1. Find all global variables prefixed with 'xxx_' and stick them in an array (for e.g.)
  2. build a query string using the name value pairs as follows: xxx_glob_var1=value1&xxx_glob_var2=value2 etc

How do I do this?

Rich
  • 5,603
  • 9
  • 39
  • 61
Stick it to THE MAN
  • 5,621
  • 17
  • 77
  • 93
  • 1
    Possible duplicate of (except for the trivial string-building part) [Javascript - dumping all global variables](https://stackoverflow.com/questions/8369338/javascript-dumping-all-global-variables) – Bergi Jul 18 '14 at 09:24
  • See this answer to [list all custom properties on `window`](https://stackoverflow.com/a/17246535/1366033) – KyleMit Jan 20 '19 at 01:50

5 Answers5

49

Or you could simply run;

Object.keys(window);

It will show a few extra globals (~5), but far fewer than the for (var i in window) answer.

Object.keys is available in Chrome 5+, Firefox 4+, IE 9+, and Opera 12, ty @rink.attendant.6

pleshy
  • 1,468
  • 2
  • 16
  • 22
38

Something like this:

function getGlobalProperties(prefix) {
  var keyValues = [], global = window; // window for browser environments
  for (var prop in global) {
    if (prop.indexOf(prefix) == 0) // check the prefix
      keyValues.push(prop + "=" + global[prop]);
  }
  return keyValues.join('&'); // build the string
}

A test usage:

var xxx_foo = "foo";
xxx_bar = "bar";
window.xxx_baz = "baz";

var test = getGlobalProperties('xxx_');
// test contains "xxx_baz=baz&xxx_bar=bar&xxx_foo=foo"
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
5

In some cases you may want to find non-enumerable properties; therefore for..in won't work (spec, about chrome) and neither would Object.keys as both only use enumerable keys. Notice that for..in is different to in but we can't use this to iterate.

Here is a solution using Object.getOwnPropertyNames (compatibility is IE9+). I've also added support for when you do only want enumerable properties or if you want to search another in context (not global).

function findPrefixed(prefix, context, enumerableOnly) {
    var i = prefix.length;
    context = context || window;
    if (enumerableOnly) return Object.keys(context).filter( function (e) {return e.slice(0,i) === prefix;} );
    else return Object.getOwnPropertyNames(context).filter( function (e) {return e.slice(0,i) === prefix;} );
}
findPrefixed('webkit');
// ["webkitAudioContext", "webkitRTCPeerConnection", "webkitMediaStream", etc..

Then if you want to join e.g.

findPrefixed('webkit').map(function (e) {return e+'='+window[e];}).join('&');
// "webkitAudioContext=function AudioContext() { [native code] }&webkitRTCPeerConnection=function RTCPeerConnection() etc..
Community
  • 1
  • 1
Paul S.
  • 64,864
  • 9
  • 122
  • 138
3

You could do something like this:

for (var i in window) {
    // i is the variable name
    // window[i] is the value of the variable
}

Though with this, you'll get a bunch of extra DOM properties attached to window.

jimyi
  • 30,733
  • 3
  • 38
  • 34
1

In my case, the two top answers didn't work, thus I am adding another answer, to highlight the comment of Dan Dascalescu:

Object.keys(window);

When I executed it, it gave:

top,location,document,window,external,chrome,$,jQuery,matchMedia,jQuery1113010234049730934203,match_exists,player_exists,add_me,isLetter,create_match,delete_me,waiting,unsure,refresh,delete_match,jsfunction,check,set_global,autoheight,updateTextbox,update_match,update_player,alertify,swal,sweetAlert,save_match,$body,value_or_null,player,position,ability,obj_need_save,xxx_saves,previousActiveElement

where the player, positon, ability, obj_need_save, xx_saves are my actual global variables.


I just saw that there exists a similar answer to another question.

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305