HTML5 and CSS3, Second Edition

(singke) #1
That’s it. As long as that’s set, a modern browser will have no trouble hitting
that service and handling requests. It’ll send an “Origin” header, and the
server will check that header against what it allows. If there’s a match, the
request goes through.

Since all the work is done on the server, that’s as far as we’ll go on this topic.
But if you visit http://enable-cors.org/, you’ll find other valuable information,
including how to configure your servers.

11.3 Web Workers


We use JavaScript for all of our client-side coding, but if a task takes a long
time, we force the user to wait until the task has finished. Sometimes this
even causes the user interface to become unresponsive. Web workers solve
this problem by creating a simple way to write concurrent programs, or at
least offload long-running tasks to the background.

Web workers are not part of the HTML5 specification, but if you need to do
some background processing on the client side you should give web workers
a closer look.^4

Let’s look at a trivial example. We’ll use web workers to fetch data from
YouTube’s public application programming interface (API), displaying a
thumbnail that, when clicked, plays a video from YouTube. When it’s done,
it’ll look like the following figure.

Figure 39—Videos fetched with web workers


YouTube’s public API supports JavaScript Object Notation with Padding
(JSONP), which means that instead of making an XmlHttpRequest like we
would normally, we append our keywords to the URL for the Search API along
with the name of the callback function, and add it to a <script> tag in our


  1. http://www.whatwg.org/specs/web-workers/current-work/


report erratum • discuss

Web Workers • 243


Download from Wow! eBook <www.wowebook.com>

Free download pdf