1 Replies - 710 Views - Last Post: 12 September 2012 - 07:15 AM

#1 hashimserag  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-August 12

passing a string between javascript and a web worker

Posted 12 September 2012 - 01:16 AM

Hey, I'm making a simple 'insert into database' application using javascript, except that im not inserting the text i want into database by a php file connected to the database, im passing the text from one javascript file to a web worker, this web worker would send the text to the php file that connects to the database on the server. why am i doing this? the company needs the application to be more secured so this web worker is going to do all the work with the server while the interface only sends the text to the web worker.
here is my code of the index page index.htm :
<!DOCTYPE html>
<html>
<body>
<form>
write ur text here: <input type="text" name="UrText" />
</form> 
<button onclick="startWorker()">Start Worker</button> 
<button onclick="stopWorker()">Stop Worker</button>
<br /><br />

<script>
var w;

function startWorker()
{
if(typeof(Worker)!=="undefined")
  {
  if(typeof(w)=="undefined")
  {
  w=new Worker("worker.js");
  }
  w.postMessage = function(event){
	  document.getElementById('UrText').value;
//here i send the text from the text field to the worker
	  
  };
    };
  }
}

function stopWorker()
{ 
w.terminate();
}
</script>

</body>
</html>



and that is the code for my worker file worker.js:
function UploadText()
{ var SentData;
	onmessage = function ({
/*here is supposed to get the message but its giving me an error undefined UrText, im pretty sure its totally wrong i wanna knw how to do it*/
	document.getElementById('UrText').value;
	})
	//this is the ajax code part to send the text to the php file
	​var ajax = new XMLHttpRequest​(),
	    params = "SentData=" + escape('UrText');
	ajax.open("POST", "fileupload.php", true);
	ajax.send(params);	
}




also i need to know if im doing it right sending it to the php file that way?..and if im wrong n im pretty sure im wrong how can i do it? thank you. =)

sorry for the shallow question, my question is how can i pass the text from the javascript to the worker? its not working with me this way, also how to pass it from the worker to the php file as well after the worker recieving it? thank you

Is This A Good Question/Topic? 0
  • +

Replies To: passing a string between javascript and a web worker

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: passing a string between javascript and a web worker

Posted 12 September 2012 - 07:15 AM

View Posthashimserag, on 12 September 2012 - 10:16 AM, said:

/*here is supposed to get the message but its giving me an error undefined UrText, im pretty sure its totally wrong i wanna knw how to do it*/

since the Worker does its job in the background, how it is supposed to have access to the HTML document?

besides that, you never send a message to the Worker to make it start (and there is no event listener for that either).

you may want to read Mozilla’s guide about Workers: https://developer.mo...ing_web_workers
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1