Skip to content

JavaScript Coding Standards

Jason Offutt edited this page May 2, 2013 · 6 revisions

Adding JS files to the page

If you're developing a block, and you need to register a JavaScript on the page in a way that will ensure that it does not get added more than once, there are a couple different ways to do it. We've exposed a method call on the CurrentPage object that will allow you to register your external JS file without worrying about collisions.

protected void OnLoad( EventArgs e )
{
    base.OnLoad( e );
    this.CurrentPage.AddScriptLink( this.Page, "~/Scripts/path-to-your-file.js" );
}

Under the covers, this method uses ASP.NET's built in ScriptManager control to handle registering all JavaScript with the current page, and will ensure the same script file does not get added to the DOM more than once.

JS code style guide

Crockford's style guide, JSLint and JSHint

Patterns to follow, advanced tips

Common "Gotchas"

  • Manual variable hoisting
  • Brace placement
  • with operator
  • When it's OK to use eval

Why this isn't always the current object in scope

Overview of the module pattern

Overview of the prototype chain

Further reading

JavaScript: The Good Parts

Clone this wiki locally