42
votes

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?

5
Possible duplicate of (except for the trivial string-building part) Javascript - dumping all global variables - Bergi

5 Answers

38
votes

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"
43
votes

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

5
votes

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..
3
votes

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.

1
votes

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.