These forums are currently read-only due to receiving more spam than actual discussion. Sorry.

It is currently Sat Dec 02, 2017 4:11 pm Advanced search

Webworkers importScripts function

Here you can discuss things related to HTML and the Web in general that do not fit in to other categories.

Webworkers importScripts function

Postby rfairweather » Wed Nov 03, 2010 9:49 pm

Hello everyone,

I am attempting to create a page that has a webworker constantly finding gps locations.

I initiated the worker using this code:

Code: Select all
var worker1 = new Worker('Scripts/worker1.js');
         worker1.onmessage = function (event) {
               alert(event.data);
         };


and then in the Scripts folder, I created a js file called worker1.js which has the following code:

Code: Select all
importScripts('http://maps.google.com/maps/api/js?sensor=true' type='text/javascript');

function sendBack(message) {
   this.postMessage(message);
}

sendBack("sup");


The code without the importScripts line functions as expected, but with the line included it does not run. If I were to use the <script> tag in the index.html file, using the same url, it works fine as well. Am I not using the importScripts function properly?

its worth noting that it also does not work without the type= part removed either.

Thanks in advance!
rfairweather
<h6>
 
Posts: 2
Joined: Wed Nov 03, 2010 9:44 pm

Postby zcorpan » Thu Nov 04, 2010 2:57 pm

type= should not be there.

The script you're trying to include uses 'window' and 'document' which are not available to workers.
zcorpan
<article>
 
Posts: 807
Joined: Tue Feb 06, 2007 8:29 pm
Location: Sweden

Postby rfairweather » Thu Nov 04, 2010 5:55 pm

Thanks for your reply.

I got the type= from http://code.google.com/p/pmrpc/wiki/PmrpcApiDocs

is there a known work around for having a script like that then? I can't exactly change anything in the google side script
rfairweather
<h6>
 
Posts: 2
Joined: Wed Nov 03, 2010 9:44 pm

Postby zcorpan » Thu Nov 04, 2010 9:12 pm

rfairweather wrote:Thanks for your reply.

I got the type= from http://code.google.com/p/pmrpc/wiki/PmrpcApiDocs
I added a comment on that page.

rfairweather wrote:is there a known work around for having a script like that then? I can't exactly change anything in the google side script
Well you can't pull it in from a worker, you have to reference it from a normal <script> in the document. You can then post messages to your worker and let it do something and post the result back to the document.
zcorpan
<article>
 
Posts: 807
Joined: Tue Feb 06, 2007 8:29 pm
Location: Sweden


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 2 guests