views:

38

answers:

2

Hello, Do you know of a way to use JQuery for getting JavaScript elements of a web page? For example, getting all JavaScript function names that are used in page and so on. Thank you!

A: 

not entirely sure if this works but...

$("script").remove();

doesnt remove only functions but should remove all script elements completely.

EDIT

nvm thought i read you wanted to remove the functions

red-X
Try reading the question again. Now read your answer. Any relation?
Darin Dimitrov
A: 

Getting all the function names is something that jquery should not be needed for--it is more of a language issue.

You can get all of the functions and variables in the global scope by looking at the window object, since everything in the global scope in the browser is really a property of this object. However, this will contain all of the variables in the global scope, including ones there by default and not added by another script.

You can loop through all of the properties of the window object with a for...in loop. Here is an example:

    for (var obj in window) {
        // obj is just the name of each property
        // do stuff with window[obj]
    }

However, if this is not strictly necessary, you should really avoid it. It's possible, but I would think twice before doing this and would be very careful.

Tikhon Jelvis
Thanks for your answer. Unfortunately the code above will not work on IE only chrome and Firefox. Do you have any idea how can I make it to work on IE?
Avi Y
@Avi, that's an old bug, that unfortunately has no workaround. IE can't enumerate any function declaration (e.g.: `function foo () {}`) made in the global scope, the properties are created in the global object, but `for-in` doesn't visit them. More info [here](http://blogs.msdn.com/b/ericlippert/archive/2005/05/04/414684.aspx).
CMS
@CMS - thank you!
Avi Y