views:

57

answers:

1

What's the best way to know if the running browser supports web-workers ?

is it

try { new Worker(); } catch (e) { /* it does not */ }

Thanks

+3  A: 

This is the code the script Modernizr uses:

tests[webWorkers] = function () {
    return !!window.Worker;
};
Yi Jiang
super. Thanks...
Lx1
A bit naive (I bet I have pages that define something called `Worker` in some of my older stuff), but...
T.J. Crowder
Dive into HTML5 suggests the same code (see http://diveintohtml5.org/everything.html). @T.J.: would your thing called `Worker` end up on the `window` object though?
Paul D. Waite
@Paul: Yes. If you declare a `var` at page scope, it becomes a property of `window`. My early stuff, like a lot of early stuff (and even a lot of stuff today) declared lots of `var`s at page scope. (Now I use a scoping function so most of my stuff exposes no public symbols at all.)
T.J. Crowder
@T.J.: ah, right, that is awkward, I hadn‘t realised.
Paul D. Waite