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.
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
Most helpful comment
Just put this line at the top of your file to disable the warning:
But the best way is to use "#" into the href and to return "false" for all javascript events: