JSLint and Bookmarklets
Asked Answered
E

2

13

I'm running JSLint checks in Rhino using jslintant.js.

I found something a bit strange and was wondering if I could gets some input from other programmers. Basically, the following line gets a JSLint 'Script URL' error:

var a = '<a href="javascript:alert(\'I am a bookmarklet\');" >Drag me to your Toolbar</a>';

Error:

Lint at line 124 character 35: Script URL.

I've gone into the code that Douglas Crockford wrote in fulljslint.js and found that indeed he is testing for this as follows:

// javascript url
jx = /(?:javascript|jscript|ecmascript|vbscript|mocha|livescript)\s*:/i,

So, given this constraint and the fact that drag and drop bookmarklets only use the HREF attribute of the A tag. How are we meant to dynamically create bookmarklets that pass a JSLint test?

Thanks for your input.

Epigraphic answered 1/11, 2011 at 13:4 Comment(0)
A
16

JSLint can be too picky at times. In this case I would suggest trying to go for an workaround. The following two bypass the regex test by splitting the string into smaller parts:

var x = ['<a href="javascript', ':', 'stuff'].join('');

or

var tmp = '<a href="javascript'
var x = tmp + ':stuff"';

Unfortunately, you can't just concatenate the string literals with + (as the old version of this answer used to suggest) because new versions of JSLint added some special code to detect that:

var x = 'javascript' + ':' + 'stuff'; // JSLint might still give a warning here.

Edit: If you are using JSHint instead of JSLint you can set the "scripturl" option to supress this class of warnings without needing to use a workaround:

/*jshint scripturl:true*/
var x = 'javascript: foo()';
Acaudal answered 1/11, 2011 at 13:8 Comment(8)
Thanks missigno, I was thinking 'javascript' + ':' + bookmarkletJs myself but I'm wondering if there is a more elegant approach.Epigraphic
The only more elegant thing that I can think of is if there is a JSLint option to disable the warning. However, I'm not on the mood to trawl though the JSLint options today ;)Acaudal
This answer no longer works, plus, there is no option to disable the check! Really annoying. And to make it worse, there is no general way any more in jslint to ignore code parts. So summed up: no way to suppress a warning for this in jslint.Krasnoff
@weiglt: Looks like JSLint is bweing a pedantic bastart again. In addition to the workaround with an inner variable you can also use Array.join or use JSHint instead.Acaudal
@weiglt: What I'm finding really wierd is that JSLint now apparently catches javascript' + ':foo' but it does not give an error if there is some code before the "javascript part": 'href=javascript' + ':stuff'Acaudal
JSHint complains about my (correct) comment headers, so i didn't even want to waste a minute with it.Krasnoff
@weiglt: What kind of warning are you getting? I don't remember JSHint ever caring about my comment structure and theres usually a flag to ignore any warning you dont want.Acaudal
@weiglt: as I mentioned previously, it depends if the "javascript" is at the start of the string or not.Acaudal
R
0

I ran into this issue too, like someone suggested before you can split it into parts to get around the warning but

var x = 'javascript' + ':';

won't work. A simpler way than the previously suggested method is:

var x = 'javascript' + ':'.toLowerCase() + 'alert("my action")';
Roselleroselyn answered 12/12, 2014 at 9:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.