{"action":"closed","issue":{"url":"https://api.github.com/repos/jsbin/jsbin/issues/2891","repository_url":"https://api.github.com/repos/jsbin/jsbin","labels_url":"https://api.github.com/repos/jsbin/jsbin/issues/2891/labels{/name}","comments_url":"https://api.github.com/repos/jsbin/jsbin/issues/2891/comments","events_url":"https://api.github.com/repos/jsbin/jsbin/issues/2891/events","html_url":"https://github.com/jsbin/jsbin/issues/2891","id":178350297,"number":2891,"title":"\"window is not defined error\" on String based webworker example","user":{"login":"BlessYAHU","id":317033,"avatar_url":"https://avatars.githubusercontent.com/u/317033?v=3","gravatar_id":"","url":"https://api.github.com/users/BlessYAHU","html_url":"https://github.com/BlessYAHU","followers_url":"https://api.github.com/users/BlessYAHU/followers","following_url":"https://api.github.com/users/BlessYAHU/following{/other_user}","gists_url":"https://api.github.com/users/BlessYAHU/gists{/gist_id}","starred_url":"https://api.github.com/users/BlessYAHU/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/BlessYAHU/subscriptions","organizations_url":"https://api.github.com/users/BlessYAHU/orgs","repos_url":"https://api.github.com/users/BlessYAHU/repos","events_url":"https://api.github.com/users/BlessYAHU/events{/privacy}","received_events_url":"https://api.github.com/users/BlessYAHU/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2016-09-21T13:41:45Z","updated_at":"2016-09-26T18:51:28Z","closed_at":"2016-09-26T18:51:28Z","body":"creating a webworker from a string is giving \"Uncaught ReferenceError: window is not defined\" error.\r\n\r\njsFiddle: http://jsbin.com/darati/edit?js,console,output\r\ncode:\r\n<pre>\r\nfunction worker() {\r\n  \r\n  var n = 1;   \r\n  postMessage('n is ' + n);\r\n  search: while(n < 2000) {\r\n    n += 1;\r\n    for (var i = 2; i <= Math.sqrt(n); i += 1) {  \r\n      if (n % i == 0) {     \r\n        continue search; \r\n      }  \r\n    }  // found a prime!\r\n    postMessage(n); \r\n  }\r\n}\r\n\r\nvar code = worker.toString();\r\ncode = code.substring(code.indexOf(\"{\")+1, code.lastIndexOf(\"}\"));\r\n\r\n\r\nvar blob = new Blob([code], {type: \"application/javascript\"});\r\nvar aworker = new Worker(URL.createObjectURL(blob));\r\n\r\naworker.addEventListener('message', function(m) {\r\n  document.body.innerHTML += m.data;\r\n  console.log(\"msg %o\", m.data);\r\n});\r\n</pre>\r\n\r\njsbin source image:  \r\n![image](https://cloud.githubusercontent.com/assets/317033/18713202/7415368a-7fdf-11e6-89d3-a7fd201e41ac.png)\r\n"}}
{"actor":{"display_login":"remy"}}