javascript:void() will throw a javascript error - you need to use javascript:void(0)

 

This is one of those javascript errors that makes me shake my head a bit, but with more and more Ajax style apps being built in Asp.Net, I have started seeing this quite a bit.

If you have something like this:

<a href="javascript:void()" onclick="doMyFunction()">Click Here</a>

It will throw the following javascript error (in Firefox, at least).

void

 

The solution is to ALWAYS pass the void function a 0 - like this:

<a href="javascript:void(0)" onclick="doMyFunction()">Click Here</a>

 

More later - joel

Published Tuesday, January 20, 2009 9:55 AM by joelvarty
Filed under: , ,

Comments

# re: javascript:void() will throw a javascript error - you need to use javascript:void(0)

Tuesday, January 20, 2009 11:39 AM by Michael Schwarz

Another solution would be "javascript:;" which is a little bit shorter.

# re: javascript:void() will throw a javascript error - you need to use javascript:void(0)

Tuesday, January 20, 2009 12:10 PM by Mark McDonnell

Dear god I hope you're not actually using 'inline JavaScript', this is 2009!

Seriously though, why aren't you using unobtrusive JavaScript?

# re: javascript:void() will throw a javascript error - you need to use javascript:void(0)

Tuesday, January 20, 2009 12:57 PM by Dave T

Mark,

Thanks for the reminder, I thought it was 2008.

# re: javascript:void() will throw a javascript error - you need to use javascript:void(0)

Tuesday, January 20, 2009 1:01 PM by joelvarty

Thanks for the comment,  Mark, and the heads up on what year it is ;) - this example is just an illustration of a common problem - I think the jury is still out on the best way of implementing unobtrusive JavaScript at this point.

My next example may just use the jQuery methodology that I recommend, though, since it matches how i like to work in c#.

# re: javascript:void() will throw a javascript error - you need to use javascript:void(0)

Monday, January 26, 2009 2:56 PM by Storm

There is another more serious error I have with a web page called www.tagged.com. I cannot add people to my friends list and when I try, I get this error:

// vim:set ts=4 sw=4 sts=4 foldmethod=marker syntax=javascript expandtab:

TAGGED.namespace('api')

/**

* Handle tagged API calls

*

* @author terry chay <tychay@tagged.com> rewrote most of it to be cleaner.

*/

TAGGED.api = {

   queue: [],

   defer: [],

   // {{{ - dequeue()

   /**

    * Play back a queue of actions.

    *

    * Note that this is automatically dequeued by call().

    */

   dequeue: function() {

       var temp = this.queue;

       this.queue = []; //clear the array

       this.call(temp);

   },

   // }}}

   // {{{ - deferDequeue()

   /**

    * Play back the defered queue also.

    *

    * Note that this is automatically dequeued by call().

    */

   deferDequeue: function() {

       this.queue = this.queue.concat(this.defer);

       this.defer = []; //remove from queue

       this.dequeue();

   },

   // }}}

   // {{{ - call(postObj,postCall)

   /**

    * @param postObj can be an object that contains the key value pairs to be

    * passed to the handler, an array of such objects or a string encoding of

    * the parameters directly. For example:

    * <code>

    * postObj = [

    *    {

    *     method : 'tagged.profile.get_box',

    *     callback : 'TAGGED.profile.loadBox',

    *     box_type : 'basicinfo',

    *     ...

    *    },

    *    {

    *     method : 'tagged.profile.get_box',

    *     callback : 'TAGGED.profile.loadBox',

    *     box_type : 'basicinfo',

    *     ...

    *    }

    * ];

    * </code>

    *

    * Addded app_id support, use createQueryString() on get string.

    * (20080327 tychay)

    *

    * @param postCall is optional, for when you want to run a callback function

    *     after all calls are returned

    * @todo please upgrade code to obj.api_signature = '';use YUI cookies in utils

   */

   call: function(postObj, postCall) {

       // auto dequeue

       if (this.queue.length) { this.dequeue(); return; }

       var postData = '\n'; //first line is ignored, always "mux"'d

       var getData = {

           application_id: 'user',

           format: 'jsonp'

       };

       if (postObj.constructor == Array) {

           for (var i = 0, len=postObj.length; i < len; ++i) {

               postData += TAGGED.api.createQueryString(postObj[i],true) + "\n";

           }

       } else if (postObj.constructor == String) {

           postData += postObj + '\n';

       } else {

           postData += TAGGED.api.createQueryString(postObj,true) + "\n";

       }

       getData.session_token = YAHOO.util.Cookie.get('S');

       TAGGED.api.callback.argument = postCall;

       var request = YAHOO.util.Connect.asyncRequest(

           'POST',

           '/api/?'+TAGGED.api.createQueryString(getData),

           TAGGED.api.callback,

           postData

       );

   },

   // }}}

   // {{{ - callback(o)

   /**

    * Handle ajax return

    */

   callback: {

       success: function(o) {

           if (!o || !o.responseText) {

               alert("An error occurred. Please refresh the page and try again. Response is empty.");

               return false;

           }

           try {

               eval(o.responseText);

               //console.log('end');

           } catch(x) {

               alert("An error occurred. Please refresh the page and try again. Exception in eval. " + x.toSource());

               return false;

           }

           if (o.argument) { o.argument(); }

           return true;

       },

       failure: function(o) {

           //console.log("An error occurred. Please refresh the page and try again. Reponse failed");

           // no failure alert since this function is called if a user clicks

           // away during an ajax response.

       }

   },

   // }}}

   // {{{ + nullResponseHandler()

   /**

    * Since all calls are JSONP now, allows a call to have a null function

    * response.

    */

   nullResponseHandler : function() {},

   // }}}

   // {{{ + createQueryString(obj,appendRequired)

   /**

    * Encode a javascript object into components

    *

    * should encode the name; made the removal of trailing '&' more efficient

    * (20080327 tychay)

    */

   createQueryString: function (obj,appendRequired) {

       var str = '';

       if (appendRequired) {

           //obj.inc = new Date().valueOf();

           obj.api_signature = '';

           if (TAGGED.guid) {

               obj.track = TAGGED.guid;

           }

           // make sure method is the first call {{{

           if (obj.method) {

               str += 'method='+encodeURIComponent(obj.method)+'&';

               delete obj.method;

           } else {

               alert("An error occurred. Please refresh the page and try again. Method is missing.");

               return '';

           }

           // }}}

       }

       for (i in obj) {

           str += encodeURIComponent(i) + '=' + encodeURIComponent(obj[i]) + '&';

       }

       // Remove trailing '&'

       if (str.length) {

           return str.substr(0,str.length-1);

       }

       return str;

   }

   // }}}

};

// }}}

Can anyone possibly help me?

# re: javascript:void() will throw a javascript error - you need to use javascript:void(0)

Thursday, August 13, 2009 6:41 PM by Roy Flowers

I think a more appropriate way to express

<a href="javascript:void()" onclick="doMyFunction()">Click Here</a>

would be

<a href="javascript:doMyFunction()">Click Here</a>

# re: javascript:void() will throw a javascript error - you need to use javascript:void(0)

Wednesday, August 26, 2009 2:54 AM by Melody

When I go to watch the yahoo news videos I get "javascript:void" at the bottom in the gray area. This only started in the last 3 days. I've done nothing different to my pc for this to happen. I have IE 8. I spent 2 hours with dell and they could not solve the problem. We got rid of 8 and went to IE 7 but that did not solve the problem so I reinstalled IE8.

Any ideas what I can do. I'm not computer savy so please give me super easy directions:O) I sure would appreciate it if you could help. Thanks.

# re: javascript:void() will throw a javascript error - you need to use javascript:void(0)

Thursday, August 27, 2009 6:40 AM by Ravi

Hi

I am facing the same problem as Melody. Only difference is that I get this message when I try to use my orkut account and post messages or try to scrap.

I had IE 6 version earlier, and installed IE 8. Could it be due the IE version ?

Can some one help ?

# re: javascript:void() will throw a javascript error - you need to use javascript:void(0)

Friday, December 10, 2010 11:24 AM by Boot to the head

Upgrading/reinstalling IE doesn't help?  Well, have you tried instead using Firefox?  Or Opera?  Or Safari?  Or Chrome?  One of those solutions will usually solve all your IE-related problems.

# re: javascript:void() will throw a javascript error - you need to use javascript:void(0)

Thursday, March 31, 2011 7:01 AM by Imran

thanks man it's worked.

Leave a Comment

(required) 
(required) 
(optional)
(required)