javascript dom 检查代码在 IE 上不起作用

发布于 2024-09-29 21:40:45 字数 353 浏览 0 评论 0原文

这个问题是向 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

季末如歌 2024-10-06 21:40:45

找不到任何与此等效的 IE8,但您可以使用此代码读取页面中的原始 JS 内容并解析它以查找其中的任何函数:

var arrScripts = document.getElementsByTagName("script");
for (var i = 0; i < arrScripts.length; i++)
    alert(arrScripts[i].innerHTML);

不像 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:

var arrScripts = document.getElementsByTagName("script");
for (var i = 0; i < arrScripts.length; i++)
    alert(arrScripts[i].innerHTML);

Not elegant as what Chrome or FF offer but it will still work.. hope someone can come with better way though. :)

妥活 2024-10-06 21:40:45

您可以使用 typeof() 直接找出它是否是一个函数,而不必将其转换为字符串:(

for(var x in window){ 
      val = window[x];
     if(typeof(val)=="function"){  //only functions 
        alert(""+val);
     }
 } 

注意,我已将其转换为 alert( ) 所以你可以看到它仍然在做同样的事情;这取决于你的目标,你甚至可能不需要这样做)

You can use typeof() to find out directly whether it's a function, instead of having to convert it to a string:

for(var x in window){ 
      val = window[x];
     if(typeof(val)=="function"){  //only functions 
        alert(""+val);
     }
 } 

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

我们的影子 2024-10-06 21:40:45

关于本机方法:
在 IE8 中,您可以访问 Window.prototype 来获取它们:

<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=100" >
<title>Test</title>
</head>
<body>
<ul style="font:normal 12px monospace;">
<script type="text/jscript">
<!--
try{
var win=Window.prototype;
for(var k in win)document.write('<li><strong style="color:blue">'+k+':</strong>'+win[k]+'</li>');
}catch(e){alert('[Window.prototype] not supported');}
//-->
</script>
</ul>
</body>
</html>

请注意,IE8 必须在 IE8 模式下运行,否则 Window (大写 W )将是未知的。

如果用户创建的函数显式分配给窗口对象,则应该可用。

a=function(){}//will not work
function b(){}//will not work
window.c=function(){}//this will work

Regarding to the native methods:
In IE8 you can access Window.prototype to get them:

<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=100" >
<title>Test</title>
</head>
<body>
<ul style="font:normal 12px monospace;">
<script type="text/jscript">
<!--
try{
var win=Window.prototype;
for(var k in win)document.write('<li><strong style="color:blue">'+k+':</strong>'+win[k]+'</li>');
}catch(e){alert('[Window.prototype] not supported');}
//-->
</script>
</ul>
</body>
</html>

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.

a=function(){}//will not work
function b(){}//will not work
window.c=function(){}//this will work
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文