Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List all properties of window object?

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?

like image 740
Graham Avatar asked Apr 29 '12 17:04

Graham


People also ask

How do you list all properties of an object?

To get all own properties of an object in JavaScript, you can use the Object. getOwnPropertyNames() method. This method returns an array containing all the names of the enumerable and non-enumerable own properties found directly on the object passed in as an argument.

What are different types of window object?

Methods of window object displays the alert box containing message with ok button. displays the confirm dialog box containing message with ok and cancel button. displays a dialog box to get input from the user. opens the new window.

What are object properties?

Object properties differentiate objects from other objects. The basic properties of an object are those items identified by its four-part name (name, type, instance, and version) and also include owner, status, platform, and release.

How many types of object properties are there?

Objects have two types of properties: data and accessor properties.


2 Answers

for (var prop in window)
    console.log(prop);

That's what you need?

like image 74
Chopin Avatar answered Oct 05 '22 18:10

Chopin


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/

like image 33
user1106925 Avatar answered Oct 05 '22 20:10

user1106925