The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

peterd (6) [Avatar] Offline
#1
JSHint is reporting some errors/warnings in Listing 2.3 (mainly missing ";" after `script.src = url`) and missing `{` and `}` from the `if (script.addEventListener)` statement.

Oddly, I'm not seeing a warning for the fishy `attachEvent('onreadystatechange', readyHandler = function () {` line. I thought JSHint would complain that readyHandler was not previously defined using var/function. May be worth asking Anton why that isn't an error/warning from JSHint, actually.

Tweaked source below:

function loadScript(url, callback) {
var script = document.createElement('script');
script.type = 'text/javascript';
script.async = true;
script.src = url;

var entry = document.getElementsByTagName('script')[0];
entry.parentNode.insertBefore(script, entry);

if (script.addEventListener) {
script.addEventListener('load', callback, false);
} else {
script.attachEvent('onreadystatechange', readyHandler = function () {
if (/complete|loaded/.test(script.readyState)) {
callback();
script.detachEvent('onreadystatechange', readyHandler);
}
});
}
}
benvinegar (68) [Avatar] Offline
#2
Re: Listing 2.3 vs JSHint
Okay, good catch. I think I'll change that function definition to not use the fn = name syntax anyways.

Out of curiosity, are you just copy/pasting these into the JSHint website?
peterd (6) [Avatar] Offline
#3
Re: Listing 2.3 vs JSHint
Yeah, I'm just manually copy/pasting in to the JSHint website to quickly check if the code validates. There were a couple other examples which were missing the odd semicolon which I didn't bother noting. If you want I can post those comments too, but if it doesn't look like I/we have access to the latest book source it may not be worth me filing issues for this stuff.

-peter