Href for JavaScript links: “#” or “javascript:void(0)”?

Useful stuff – what to do with a javascript link:

I use javascript:void(0).

Three reasons. Encouraging the use of # amongst a team of developers inevitably leads to some using the return value of the function called like this:

function doSomething() {
    //Some code
    return false;

But then they forget to use return doSomething() in the onclick and just use doSomething().

A second reason for avoiding # is that the final return false; will not execute if the called function throws an error. Hence the developers have to also remember to handle any error appropriately in the called function.

A third reason is that there are cases where the onclick event property is assigned dynamically. I prefer to be able to call a function or assign it dynamically without having to code the function specifically for one method of attachment or another. Hence my onclick (or on anything) in HTML markup look like this:



onclick="someFunc.apply(this, arguments)"


Main link:

Don’t name your input controls “submit”

I just discovered what happens when you name your input controls “submit”.  I have an aspx page with a button on it.  The button had an ID of “submit”.  The page also has a logout link at the top of the page, which stopped working.  After debugging for a while I realized the __doPostBack function was throwing an exception.  The code looks like:

function __doPostBack(eventTarget, eventArgument) {
  if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
    theForm.__EVENTTARGET.value = eventTarget;
    theForm.__EVENTARGUMENT.value = eventArgument;

The error was that theForm.submit wasn’t a method.  So I set a breakpoint to check out what submit was, and it turned out to be my input button!  The one I stupidly gave an ID of “submit”.  So after a quick ID change everything started working.