I am trying to upload images using the webworker api. I have large images on my html page, so it takes 5 minutes to download all the images, so I use webworker to upload the images.
here is the technique.
I keep the src attribute of the entire img tag empty on the html page.
All images have a unique identifier for each img, for example id = events_all_1_01 , that is, the src image will be "images / keywords / events / all / 1 / 01.jpg ". ie last part events / all / 1 / 01.jpg is id.
Main.html File
<body> <script src="js/modernizr.custom.js"></script> <script language="javascript"> window.onload = function(){ if (Modernizr.webworkers) { var worker = new Worker('js/webworker/test_ww_23_04.js'); worker.onmessage = function(event) { var url = event.data.replace(/_/g, "/"); var image_src = "pictures/keywords/"+url+".jpg"; var img = new Image(); img.src = image_src; img.onload = function(){ </script> <div id="wrapper" style="height: 500px;width: 200px;overflow-y: auto;border: 1px solid gray;"> <div id="pictures1"> <div class="effect-1"> <div><img src="" id="events_all_1_01" width="150" height="100"></div> <div><img src="" id="events_all_1_02" width="150" height="100"></div> <div><img src="" id="events_all_1_03" width="150" height="100"></div> <div><img src="" id="events_all_1_04" width="150" height="100"></div> </div> <hr/> <div class="effect-2"> <div><img src="" id="events_all_2_01" width="150" height="100"></div> <div><img src="" id="events_all_2_02" width="150" height="100"></div> <div><img src="" id="events_all_2_03" width="150" height="100"></div> <div><img src="" id="events_all_2_04" width="150" height="100"></div> </div> <hr/> <div class="effect-3"> <div><img src="" id="events_all_3_01" width="150" height="100"></div> <div><img src="" id="events_all_3_02" width="150" height="100"></div> <div><img src="" id="events_all_3_03" width="150" height="100"></div> <div><img src="" id="events_all_3_04" width="150" height="100"></div> </div> <hr/> <div class="effect-4"> <div><img src="" id="events_all_4_01" width="150" height="100"></div> <div><img src="" id="events_all_4_02" width="150" height="100"></div> <div><img src="" id="events_all_4_03" width="150" height="100"></div> <div><img src="" id="events_all_4_04" width="150" height="100"></div> </div> </div> </div> </body>
website code.
//var src = 'pictures/keywords/events/all/1/01.jpg'; //var id = src.substring(src.substring(0,18).length).split('.')[0].replace(/\//g, "_"); // Creating id here......events_all_1_01 //var fst = id.substring(0, id.lastIndexOf("_")+1); // get first part.... events_all_1 //var lst = parseInt(id.substr(id.lastIndexOf("_")+1)); // get last part ie imagename ..01,02,03 etc....... and convert it to int function LoadImages(currID) { setTimeout(function() { postMessage(currID); }, 100); } self.onmessage = function(event) { var currID = event.data; LoadImages(currID); };
I get the following error:
Uncaught SyntaxError: Failed to execute 'postMessage' in 'Window': Invalid target origin '' in postMessage call.
javascript html image web-worker
Developer desk
source share