Question

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!

No correct solution

OTHER TIPS

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. :)

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)

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top