views:

248

answers:

4

I have an application which use some javascript functions,

As all javascripts include in Masterpage, most of them which comes withpage are not necessary, and some of those are repeated ( cause used in some different JS file. )

So I want to check if there is a way to determine duplicate functions and remove them ?

+1  A: 

Unfortunately, unless they're named the same, or reference the same classes or IDs, this is extremely difficult. But, if it's a duplicate it should fall under one of those matches. I say this because after a major conversion in our project I went back and cleaned up all the javascript into nice namespaces and had this same issue.

This answer is going to suck, and I apologize: Ctrl+Shift+F. Search for the same function names, or the same classes/IDs if you think it has a different name/same function.

I find searching for function name, #id and .class to be the most productive. Also exclude .cs files from your search. This will minimize the time since the preceeding # and . will only appear when using in jQuery, but it's still a very manual process.

I hope someone has a better solution here, and if they do I'll feel like an idiot, but I honestly could not find a better way in VS 2008 to do this.

Nick Craver
+4  A: 

You can check if the function exists when declaring a function, but You have to change the way it works.

instead of

function foo(){ something }

do

if(window.foo===undefined){
window.foo=function(){ something }
}

and You can still call it

foo();

All this can be added in Your files with a find&replace using regexp, so I consider this easy :)

naugtur
this solution will include the function on page, and do not prevent to add the function to page
Nasser Hadjloo
There is no way to prevent loading a js file if it duplicates functions. My solution WILL prevent adding a function to a namespace when the name is used. If You want to skip loading files, then You have to create a server-side manager that would serve js files and read them on the way searching for duplicates.You could switch to loading js files dynamically, but You don't know what You're loading until it's loaded.
naugtur
A: 

If the functions from two scripts have the same name, or are named methods of an object, the first function will be replaced by the second when the second script is interpreted.

You will not have two identical functions 'taking up space', but only one.

kennebec
I know that only the last one will works, when functions are same. I want toremove duplicates to reduce page size.
Nasser Hadjloo
A: 

Its not clear from the question if you're using Webforms or MVC. In either case you want to be using a script manager of some sort.

For MVC I found this researching much the same problem: A Simple ScriptManager for ASP.NET MVC

For Webforms, erm, not sure, I have to go do my homework again

Murph
I'm Using Asp.net web form not MVC
Nasser Hadjloo