Fetching All (Javascript) Global Variables in a Page

Fetching all (javascript) global variables in a page

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"

Javascript - dumping all global variables

Object.keys( window );

This will give you an Array of all enumerable properties of the window object, (which are global variables).

For older browsers, include the compatibility patch from MDN.


To see its values, then clearly you'll just want a typical enumerator, like for-in.


You should note that I mentioned that these methods will only give you enumerable properties. Typically those will be ones that are not built-in by the environment.

It is possible to add non-enumerable properties in ES5 supported browsers. These will not be included in Object.keys, or when using a for-in statement.


As noted by @Raynos, you can Object.getOwnPropertyNames( window ) for non-enumerables. I didn't know that. Thanks @Raynos!

So to see the values that include enumerables, you'd want to do this:

var keys = Object.getOwnPropertyNames( window ),
value;

for( var i = 0; i < keys.length; ++i ) {
value = window[ keys[ i ] ];
console.log( value );
}

Getting All Variables In Scope

No. "In scope" variables are determined by the "scope chain", which is not accessible programmatically.

For detail (quite a lot of it), check out the ECMAScript (JavaScript) specification. Here's a link to the official page where you can download the canonical spec (a PDF), and here's one to the official, linkable HTML version.

Update based on your comment to Camsoft

The variables in scope for your event function are determined by where you define your event function, not how they call it. But, you may find useful information about what's available to your function via this and arguments by doing something along the lines of what KennyTM pointed out (for (var propName in ____)) since that will tell you what's available on various objects provided to you (this and arguments; if you're not sure what arguments they give you, you can find out via the arguments variable that's implicitly defined for every function).

So in addition to whatever's in-scope because of where you define your function, you can find out what else is available by other means by doing:

var n, arg, name;
alert("typeof this = " + typeof this);
for (name in this) {
alert("this[" + name + "]=" + this[name]);
}
for (n = 0; n < arguments.length; ++n) {
arg = arguments[n];
alert("typeof arguments[" + n + "] = " + typeof arg);
for (name in arg) {
alert("arguments[" + n + "][" + name + "]=" + arg[name]);
}
}

(You can expand on that to get more useful information.)

Instead of that, though, I'd probably use a debugger like Chrome's dev tools (even if you don't normally use Chrome for development) or Firebug (even if you don't normally use Firefox for development), or Dragonfly on Opera, or "F12 Developer Tools" on IE. And read through whatever JavaScript files they provide you. And beat them over the head for proper docs. :-)

Best way to find global variables in Javascript

compare the Keys in your window-object with the keys of an "empty" window-object:

(function(){
var iframe = document.createElement('iframe');
iframe.src = "about:blank";
document.body.appendChild(iframe);

var windowVars = Object.keys(iframe.contentWindow);
var globalVars = Object.keys(window).filter(key => !windowVars.includes(key));

console.log("global Vars:", globalVars);
document.body.removeChild(iframe);
})();

Now you have to search your code to find the line where they're declared.

Get all Javascript Variables?

Flanagan's "JavaScript - The Definitive Guide" gives the following on page 653:

var variables = ""
for (var name in this)
variables += name + "\n";

How do I list all global variables I have created in Chrome DevTools?

Provided the page doesn't dynamically leak new identifiers into the global scope, this hack lists all the identifiers that have been created after the window identifier, which seems a pretty good match for this purpose. myids() just lists their names, whereas myvars() collects them into an object you can inspect, to see them all in context together:

function myids() {
function notUs(i) { return i !== 'myids' && i !== 'myvars'; }
var ids = Object.keys(window);
return ids.slice(ids.indexOf('window') + 1).filter(notUs);
}

function myvars() {
function gather(a, i) { a[i] = window[i]; return a; }
return myids().reduce(gather, {});
}

It's not perfect, but better than nothing, and it should only have false positives, no false negatives.

Getting global Javascript variable in another Javascript file

Make it global;

script1.js

 var foobar = "O HAI";

script2.js

  alert(foobar);


Related Topics



Leave a reply



Submit