是否有一个 JavaScript 程序可以列出所有预定义的浏览器主机对象?
什么 JavaScript 程序将列出浏览器中“自发”提供的所有本机/主机/平台对象?
如果无法编写这样的程序,是否有其他方法可以生成这样的列表?
根据此要求澄清“本机/主机/平台对象”答案
示例:
使用window.navigator.userAgent =
“Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.3) Gecko/20100423 Ubuntu/10.04 (lucid) Firefox/3.6.3”
一些本机 JavaScript 对象(其中一些发生)是构造函数)
Array, Boolean, Date, Function, Number, Object. RegExp, String
Error, Iterator, JSON, Math
一些 DOM 宿主对象
Image, Option
一些其他平台对象
Worker, XMLHttpRequest, XPCNativeWrapper
引用:
- 平台对象在 W3C WebIDL 接口定义语言规范
- W3C HTML 5 规范
- W3C DOM
- Mozilla JavaScript 技术概述
- Mozilla DOM
- Mozilla ECMAScript 参考
- ecma262-5.com
另请参阅
这是最低限度有效的:
javascript:
alert("using:\n"+window.navigator.userAgent);
list=[];
for( i in window) list.push(i);
alert("found:\n"+list.sort().join("\t"));
list=[];
for( i in window) list.push([typeof eval("window."+i),i].join("\t"));
alert(["found:",list.sort().join("\n--------------\n")].join("\n"))
产生
using:
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.3)
Gecko/20100423 Ubuntu/10.04 (lucid) Firefox/3.6.3
found:
Components XPCNativeWrapper XPCSafeJSObjectWrapper addEventListener
alert applicationCache atob back blur btoa captureEvents
clearInterval clearTimeout close closed confirm content controllers
crypto defaultStatus directories disableExternalCapture dispatchEvent
document dump enableExternalCapture find focus forward
frameElement frames fullScreen getComputedStyle getInterface
getSelection globalStorage history home i innerHeight
innerWidth length list localStorage location locationbar
menubar moveBy moveTo mozInnerScreenX mozInnerScreenY name navigator
netscape open openDialog opener outerHeight outerWidth
pageXOffset pageYOffset parent personalbar pkcs11 postMessage
print prompt releaseEvents removeEventListener resizeBy
resizeTo routeEvent screen screenX screenY scroll scrollBy
scrollByLines scrollByPages scrollMaxX scrollMaxY scrollTo
scrollX scrollY scrollbars self sessionStorage setInterval
setResizable setTimeout showModalDialog sizeToContent status
statusbar stop toolbar top updateCommands window
和(选择性地已编辑)
found:
...
--------------
function $
--------------
function PR_normalizedHtml
--------------
function XPCNativeWrapper
--------------
function XPCSafeJSObjectWrapper
--------------
...
--------------
object Components
--------------
object Markdown
--------------
object PR
--------------
object StackExchange
--------------
...
--------------
object jQuery15205241375142988156
--------------
...
--------------
object window
--------------
...
What javascript program will list all the native / host / platform objects that are provided "spontaneously" in a browser?
If no such program can be written is there any other way of generating such a list?
Clarification of "native / host / platform objects" as requested by this answer
Examples:
usingwindow.navigator.userAgent =
"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.3) Gecko/20100423 Ubuntu/10.04 (lucid) Firefox/3.6.3"
some native JavaScript objects (some of which happen to be constructors)
Array, Boolean, Date, Function, Number, Object. RegExp, String
Error, Iterator, JSON, Math
some DOM host objects
Image, Option
some other platform objects
Worker, XMLHttpRequest, XPCNativeWrapper
references:
- platform objects are defined in W3C WebIDL interface definition language specification
- W3C HTML 5 specification
- W3C DOM
- Mozilla JavaScript technologies overview
- Mozilla DOM
- Mozilla ECMAScript references
- ecma262-5.com
See also
- What are the predefined primitive constructors?
- List DOM Documents attributes and methods using Javascript
This is minimally effective:
javascript:
alert("using:\n"+window.navigator.userAgent);
list=[];
for( i in window) list.push(i);
alert("found:\n"+list.sort().join("\t"));
list=[];
for( i in window) list.push([typeof eval("window."+i),i].join("\t"));
alert(["found:",list.sort().join("\n--------------\n")].join("\n"))
produces
using:
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.3)
Gecko/20100423 Ubuntu/10.04 (lucid) Firefox/3.6.3
found:
Components XPCNativeWrapper XPCSafeJSObjectWrapper addEventListener
alert applicationCache atob back blur btoa captureEvents
clearInterval clearTimeout close closed confirm content controllers
crypto defaultStatus directories disableExternalCapture dispatchEvent
document dump enableExternalCapture find focus forward
frameElement frames fullScreen getComputedStyle getInterface
getSelection globalStorage history home i innerHeight
innerWidth length list localStorage location locationbar
menubar moveBy moveTo mozInnerScreenX mozInnerScreenY name navigator
netscape open openDialog opener outerHeight outerWidth
pageXOffset pageYOffset parent personalbar pkcs11 postMessage
print prompt releaseEvents removeEventListener resizeBy
resizeTo routeEvent screen screenX screenY scroll scrollBy
scrollByLines scrollByPages scrollMaxX scrollMaxY scrollTo
scrollX scrollY scrollbars self sessionStorage setInterval
setResizable setTimeout showModalDialog sizeToContent status
statusbar stop toolbar top updateCommands window
and (selectively edited)
found:
...
--------------
function $
--------------
function PR_normalizedHtml
--------------
function XPCNativeWrapper
--------------
function XPCSafeJSObjectWrapper
--------------
...
--------------
object Components
--------------
object Markdown
--------------
object PR
--------------
object StackExchange
--------------
...
--------------
object jQuery15205241375142988156
--------------
...
--------------
object window
--------------
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定你想做什么,但是如果你想要 windows 对象中的所有方法(通常在没有对象实例前缀的情况下调用的方法),你可以这样做:
然后,你可以使用 stuff 数组来查看窗口元素中包含的所有内容(作为对象)。当然,不建议通过该数组使用方法。请注意,这将列出窗口元素上的所有属性。
如果您不需要其他内容,您能给我们提供一个示例,说明您希望从哪些元素获取哪些数据吗?
I'm not sure what you want to do, but if you want all methods from the windows object (methods that usually are called without an object instance prefix), you can just do this:
Then, you can use the stuff array to see ALL of what is contained in the window element (as objects). Of course it's not adviceable to use methods throught that array. Please note that this will list ALL properties on the window element.
If you wan't something else, could you provide us with an example of which data would you like to obtain from which elements?
不同的浏览器在 window 对象上公开不同的内容。 Chrome 似乎公开了几乎所有人们能想到的全局命名空间中的所有内容,包括所有内置类型,如数组和布尔值。 Firefox 似乎采取了相反的策略——尽可能少地暴露。
您可以在此小提琴中查看任何给定浏览器公开的内容: http://jsfiddle.net/jfriend00/6KRpK/< /a>.
我不知道有任何规范要求所有全局识别的名称(无论是内置类型还是像
parseInt()
这样的全局语言方法)可由脚本枚举,而不是必须位于的脚本窗口对象。因此,我认为这意味着如果您想要一种适用于所有浏览器的方法,那么您就不走运了。现在,您可以从 Chrome 中获得一个相当不错的列表(尽管其中一些是特定于 Chrome 的)。 Firefox 则不然。
这些其他 SO 帖子中涵盖了一些类似的领域:
全局用户定义函数列表在 JavaScript 中?
JavaScript:在 IE 中列出全局变量
Different browsers expose different things on the window object. Chrome appears to expose pretty much everything one could think of being in the global namespace including all the built-in types like Array and Boolean. Firefox seems to have the opposite tactic - exposing as little as possible there.
You can see what any given browser exposes in this fiddle: http://jsfiddle.net/jfriend00/6KRpK/.
I'm not aware of any specification that requires all globally recognized names (whether built-in types or global language methods like
parseInt()
) to be enumerable by a script other than the ones that must be on the window object.As such, I think this means you're out of luck if you want a method that works in all browsers. For now, you can get a pretty good list (though a few are Chrome-specific) out of Chrome. Not so out of Firefox.
Some similar territory covered in these other SO posts:
List of global user defined functions in JavaScript?
JavaScript: List global variables in IE