A tool that detects duplicate javascript function names on a web page?

Background

We have a web application in which several developers wrote several .js files that control the DOM, and the problem of duplicate function names got into our application.

Question

Can someone recommend a tool that will warn us when we accidentally write a web page with two javascript functions with the same name?

Example

HTML page

<script language="JavaScript" src="test.js" type="text/javascript"></script> <script>function foo() {alert('bar');}</script> 

test.js

 function foo() {alert('foo');} 

Since foo () is declared twice on the page, it seems that only the one that takes precedence is loaded.

The tools used seem to ignore this. Firebug shows only the loaded function. Netbeans will display both functions in the navigator (without warning), but will only look at one file at a time (i.e. I cannot specify it in the HTML file and see it and .js at the same time.) And on the Internet Developer extensions allow me to look at everything at once, but no errors or warnings. Firefox error console also does not cause any warnings or errors. And IE.

Thanks!

+7
javascript
source share
4 answers

Well, using an analyzer may not always be ideal, as it requires an additional step of copying and pasting the code and all the others into the parser, and even then I’m not sure that it will catch what you want. The proven Javascript collaborative development solution is the namespace of your code.

 var myNamespace = function (){ var myPrivateProperty; var myPrivateFunction = function(){}; return{ myPublicProperty: '', myPublicFunction: function(){} } }(); 

It is based on the Douglas Crockford module template .

Then you can call your public functions as follows:

  myNamespace.myPublicFunction(); 

And your public properties:

  myNamespace.myPublicProperty; 

Each developer can develop in his own namespace so as not to step on the code of others.

+3
source share

I often used JSLINT

In short, it is a "compiler" for JavaScript using JavaScript. I learned a lot by watching Douglas Crockford's instructional videos.

It not only checks for duplicate functions, but also global variables and a whole bunch of other things. As Douglas said in one of his videos, he only allows good JavaScript snippets

+5
source share

My solution would be a simple HTML parser for Java (I just hacked it with regular expressions, you can try http://java-source.net/open-source/html-parsers/jtidy ) and the Apache Rhino library ( http: / /www.mozilla.org/rhino/doc.html ).

It should be possible to overload the parser in Rhino to cause an error if the function is overridden.

I am using a slightly modified version of env.js ( http://jqueryjs.googlecode.com/svn/trunk/jquery/build/runtest/env.js ) John Resig to emulate the browser. This allows you to test JavaScript in JUnit tests. I can fill out forms, submit them, check page layout, etc.

See my blog for details: http://blog.pdark.de/2008/11/18/testing-the-impossible-javascript-in-a-web-page/

+1
source share

ScriptManager.RegisterClientScriptInclude (Me, Page.GetType, "jsTest", "test.js")

+1
source share

All Articles