Jshint: JSHint marks "javascript:void(0);" as a JS URL

Created on 23 Jun 2014  路  8Comments  路  Source: jshint/jshint

If I assign an element's href to javascript:void(0); (as opposed to #), like so:

$('#foobar').attr("href", "javascript:void(0);");

JSHint picks up the javascript:void(0); construct as a Script URL:

PS C:\Users\hal9000\Development\strugee.github.com> grunt lint
Running "jshint:all" (jshint) task

   js\main.js
     30 |    $('#lightbulb-icon').attr("href", "javascript:void(0);");
                                                                    ^ Script URL.

>> 1 error in 4 files

when in fact it isn't doing anything and is semantically valid. There should be an exception for these types of Script URLs.

Most helpful comment

Just put this line at the top of your file to disable the warning:

/*jshint scripturl:true*/

But the best way is to use "#" into the href and to return "false" for all javascript events:

<a href="#" onclick="doSomething();return false;">my link</a>

All 8 comments

Nevermind.

Perhaps this should be a specialized warning? I can't be the only one to have been confused by this.

You're not.

Just put this line at the top of your file to disable the warning:

/*jshint scripturl:true*/

But the best way is to use "#" into the href and to return "false" for all javascript events:

<a href="#" onclick="doSomething();return false;">my link</a>

No, the best way is to keep your JavaScript out of your markup and attributes :P

@rwaldron: rectification, indeed

$('#foobar').attr("href", "#").click(function() {
  // do it
});

Passes jslint:
$('#foobar').attr('href','javascript'+String.fromCharCode(58)+'void(0);');

@MikeGodin looks like a separate bug.

@kcampion putting # and return false is not always desirebale
in my case i dont want hash to appear

Was this page helpful?
0 / 5 - 0 ratings

Related issues

NemoStein picture NemoStein  路  7Comments

TheSavior picture TheSavior  路  3Comments

derekdata picture derekdata  路  11Comments

arian picture arian  路  7Comments

stefanuddenberg picture stefanuddenberg  路  7Comments