HTML 5 Webmasters with Multiple Arguments

I just got to the HTML5 website and now I want to pass a few arguments to my worker.

I have this on my page:

var username = document.getElementById("username").value;
var server_url = 'localhost';
w.postMessage(username,server_url);

and this is with my employee:

var username = '';
var server_url = '';

onmessage = function (e,f) {
    username = e.data;
    server_url = f.data;
}
console.log(username);
console.log(server_url);

and when I open the page that calls the worker in the browser: Uncaught TypeError: Failed to execute 'postMessage' on 'Worker': The 2nd argument is neither an array, nor does it have indexed properties.

all I want to do - is to transfer usernameand server_urlthe recipient I know that in the examples I hardcoded server_url, but in the real script was dynamic.

Please don't just say: change it, do it, but provide me the code so that I can see how it should be done, and not still need to figure it out.

+4
source share
1 answer

Post as follows:

w.postMessage({user:username,url:server_url})

:

onmessage = function (e,f) {
    username = e.data.username;
    server_url = e.data.url;
}
+4

Source: https://habr.com/ru/post/1627792/


All Articles