Time for action – using a web worker
Let's create a really simple application that gets the user's name and passes it to a web worker. The web worker will return a "hello" message back to the application. The code for this section can be found in Chapter 9/example9.1
.
Note
The web workers don't work in some browsers unless you are running them through a web server such as IIS or Apache.
First we create an application with webWorkerApp.html
, webWorkerApp.css
, and webWorkerApp.js
files. We add a text input field to the HTML asking for the user's name and a response section to display the message from the worker:
<div id="main"> <div> <label for="your-name">Please enter your name: </label> <input type="text" id="your-name"/> <button id="submit">Submit</button> </div> <div id="response" class="hidden">...