获取页面中的所有(javascript)全局变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2226007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 23:20:13  来源:igfitidea点击:

Fetching all (javascript) global variables in a page

javascript

提问by Stick it to THE MAN

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:

我想编写一个 javascript 函数来执行以下操作:

  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
  1. 找到所有以 'xxx_' 为前缀的全局变量并将它们粘贴在一个数组中(例如)
  2. 使用名称值对构建查询字符串,如下所示:xxx_glob_var1=value1&xxx_glob_var2=value2 等

How do I do this?

我该怎么做呢?

回答by pleshy

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.

它将显示一些额外的全局变量 (~5),但远少于for (var i in window)答案。

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

Object.keys 在 Chrome 5+、Firefox 4+、IE 9+ 和 Opera 12 中可用,ty @gsamaras

回答by CMS

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"

回答by Paul S.

In some cases you may want to find non-enumerableproperties; therefore for..inwon't work (spec, about chrome)and neither would Object.keysas both only use enumerable keys. Notice that for..inis different to inbut we can't use this to iterate.

在某些情况下,您可能希望找到不可枚举的属性;因此for..in不会工作规范关于 chrome,也不会Object.keys因为两者都只使用可枚举的键。请注意,这for..in与 to 不同,in但我们不能使用 this 进行迭代。

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).

这是使用的解决方案Object.getOwnPropertyNames(兼容性为 IE9+)。我还添加了对何时您只需要可枚举属性或如果您想在上下文中搜索另一个(非全局)的支持。

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

回答by jimyi

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.

尽管如此,你会得到一堆附加到 window.DOM 的额外 DOM 属性。

回答by gsamaras

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

就我而言,两个最重要的答案不起作用,因此我添加了另一个答案,以突出 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

顶部,位置,文档,窗口,外部,镀铬,$,jQuery,matchMedia,jQuery1113010234049730934203,match_exists,player_exists,add_me,isLetter,create_match,delete_me,等待,不确定,刷新,delete_match,jsfunction,check,update_global 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.

其中玩家、位置、能力、obj_need_save、xx_saves 是我的实际全局变量。



I just saw that there exists a similar answerto another question.

我刚刚看到另一个问题有类似的答案