views:

74

answers:

3

Hello, 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!

A: 

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

Shadow Wizard
This really doesn't do the same thing. Parsing out the JS in JS will be complicated and functions can be added dynamically which would not show up by parsing script tags. Also referencing `innerHTML` wouldn't catch externally loaded JS scripts.
Sam
I know it's not the same, it's just way around that might be enough for the OP needs.
Shadow Wizard
A: 

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
Dr.Molle
Thank for your answer. But if I am using window.c=function(){} this will also work in the first example I have shown. But that is not good enough. Anyway thanks for trying.
Avi Y
A: 

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)

Spudley
The problem here is not to determine if it's a function or not, the native functions and user-function won't be available in IE while walking through the window-object.
Dr.Molle
huh. really? I'm surprised. I assumed the issue must be a problem with his `startsWith()` function, since I would expect even in IE to be able to say `window[myglobalfunctionname]()` and have it run, so they ought to all be under the window object. Anyway.... I dunno .... *shrugs*
Spudley
That is the strange thing about it. If I add this: function fff(){} to my web page. Than this: window['fff'] actually works also in IE. But still, the code above will not return the fff in IE(only in firefox and Chrome).
Avi Y