javascript dom 检查代码在 IE 上不起作用
这个问题是向 JS 专家提出的。
我使用下一段代码来获取 dom 根下存在的所有 JavaScript 函数名称。
for(var x in window){
val = ""+window[x];
if(startsWith(val,"function")){ //only functions
alert(val)
}
}
虽然此代码在 Firefox 和 Chrome 上完美运行,但似乎不适用于 IE8。 IE中的window元素下好像不存在这些功能。
你知道我如何让它在 IE 上工作吗? 谢谢你!
this question is for the JS experts out there.
I use this next piece of code to get all the JavaScript function names which exist under the dom root.
for(var x in window){
val = ""+window[x];
if(startsWith(val,"function")){ //only functions
alert(val)
}
}
While this code works perfectly on Firefox and Chrome it doesn't seem to work on IE8.
It seems as if the functions do not exist under the window element in IE.
Do you have any idea how would I make it to work on IE?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
找不到任何与此等效的 IE8,但您可以使用此代码读取页面中的原始 JS 内容并解析它以查找其中的任何函数:
不像 Chrome 或 FF 提供的那么优雅,但它仍然可以工作。希望有人能提出更好的方法。 :)
Couldn't find any IE8 equivalent of this, but you can use this code to read the raw JS contents in the page and parse it to find any functions in there:
Not elegant as what Chrome or FF offer but it will still work.. hope someone can come with better way though. :)
您可以使用
typeof()
直接找出它是否是一个函数,而不必将其转换为字符串:(注意,我已将其转换为
alert( )
所以你可以看到它仍然在做同样的事情;这取决于你的目标,你甚至可能不需要这样做)You can use
typeof()
to find out directly whether it's a function, instead of having to convert it to a string:(note, I've converted it to a string for the
alert()
so you can see it's still doing the same thing; depending what you're aiming to do, you may not even need to do that)关于本机方法:
在 IE8 中,您可以访问 Window.prototype 来获取它们:
请注意,IE8 必须在 IE8 模式下运行,否则 Window (大写 W )将是未知的。
如果用户创建的函数显式分配给窗口对象,则应该可用。
Regarding to the native methods:
In IE8 you can access Window.prototype to get them:
Note that IE8 has to run in IE8-mode, otherwise Window (with uppercase W ) will be unknown.
User-created functions should be available, if are explicitly assigned to the window-object.