views:

557

answers:

1

I have a JS script (widget) which is added to other websites. Technically its similar to Google analytics. My question is: what are your advices for ensuring robustness, keeping the code from interfering with other code on the site, avoiding errors for users etc. In general, what should I know to write a professional grade widget.

Notes: I can't use any JS library such as jquery etc..

+4  A: 

I am a big fan of Peter Michaux's guide on how he write's javascript widgets

Also useful are Christian Heilmann's script configuration and the module pattern

Those are generic javascript articles and aren't specific to a single library

Other useful tricks are things like wrapping your code in an anonymous function to stop it interfering with other global libraries.

(function() {
    //Your code goes in here
})();

Regarding errors and best practice, John Resig has an interesting article on javascript strict that isn't in yet, but does have some handy information on the sort of things you should be avoiding.

If you're still coming to terms with scoping within your objects, then you might find this article on private and public variables useful as well a a bit more technical definition by Douglas Crockford

Finally, remember to run your completed code through a code quality tool

Steerpike
excellent answer, give me more of these
Nir