最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - List all properties of window object? - Stack Overflow

programmeradmin1浏览0评论

I'm looking to (dynamically) obtain a list of HTML elements the browser is currently aware of, such as HTMLPreElement, HTMLSpanElement etc. These objects are global, i.e.

console.log('HTMLPreElement' in window);  //=> true

So I thought I'd be able to use getOwnPropertyNames like this:

console.log(Object.getOwnPropertyNames(window));

to obtain the full list of global properties (MDN states that this returns both enumerable and non-enumerable properties).

Using the above, I get an array with around 70 property nanes. But, it doesn't include objects like HTMLPreElement - only HTMLElement. I also tried:

console.log(Object.getOwnPropertyNames(window.Window.prototype));

which brings back a bigger list (including addEventListener etc) but again, no HTMLPreElement.

So, where the heck do these HTML{Tag}Element objects reside?

I'm looking to (dynamically) obtain a list of HTML elements the browser is currently aware of, such as HTMLPreElement, HTMLSpanElement etc. These objects are global, i.e.

console.log('HTMLPreElement' in window);  //=> true

So I thought I'd be able to use getOwnPropertyNames like this:

console.log(Object.getOwnPropertyNames(window));

to obtain the full list of global properties (MDN states that this returns both enumerable and non-enumerable properties).

Using the above, I get an array with around 70 property nanes. But, it doesn't include objects like HTMLPreElement - only HTMLElement. I also tried:

console.log(Object.getOwnPropertyNames(window.Window.prototype));

which brings back a bigger list (including addEventListener etc) but again, no HTMLPreElement.

So, where the heck do these HTML{Tag}Element objects reside?

Share Improve this question edited Feb 27, 2013 at 20:37 ThinkingStiff 65.3k30 gold badges147 silver badges241 bronze badges asked Apr 29, 2012 at 17:51 GrahamGraham 6,5622 gold badges38 silver badges39 bronze badges 3
  • Looks like this does work in WebKit (with just a simple for...in) but Firefox refuses to list them. – Graham Commented Apr 29, 2012 at 18:01
  • 1 Though it's interesting to know why you can't see it in Firefox, it's interesting not less to know why do you care...? – gdoron Commented Apr 29, 2012 at 18:07
  • @gdoron - at first I simply needed a list of valid tags, but when I couldn't see them I was more interested in why they wouldn't appear. – Graham Commented Apr 29, 2012 at 18:23
Add a comment  | 

3 Answers 3

Reset to default 7
for (var prop in window)
    console.log(prop);

That's what you need?

In Firefox, it seems to be the behavior of elements that their global object is not added unless explicitly requested as a global variable or property. Perhaps Firefox lazy loads them into the environment so that they don't consume memory unless they're actually needed.

It seems that they do not show up when simply requesting the keys of the global object via Object.getOwnPropertyNames unless they've first been explicitly referenced as described above.

http://jsfiddle.net/mBAHm/

var obj = window;
while(obj){
    for(let prop of Reflect.ownKeys(obj)){
        console.log(prop);
    };
    obj = Object.getPrototypeOf(obj);
};
发布评论

评论列表(0)

  1. 暂无评论