Web worker是否支持?
我在不同的网站上发现了非常令人困惑的答案。
感谢您的帮助......
答案 0 :(得分:2)
如有疑问,请进行测试。我的Chromium版本建议答案是:
来源here及以下。
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Embedded Web Worker Test</title>
<script type="text/js-worker">
var xmlhttp = 'XMLHttpRequest: ' + ( ( 'function' === typeof XMLHttpRequest ) ? ' YES' : ' NO' )
var file_reader = 'FileReader: ' + ( ( 'function' === typeof FileReader ) ? ' YES' : ' NO' )
var websockets = 'Websockets: ' + ( ( 'function' === typeof WebSocket ) ? ' YES' : ' NO' )
var myVar = "Test results: " + xmlhttp + ", " + file_reader + ", " + websockets;
</script>
<script type="text/javascript">
function log_worker_msg(worker_msg) {
var frag = document.createDocumentFragment();
frag.appendChild(document.createTextNode(worker_msg));
document.querySelector("#worker_log").appendChild(frag);
}
</script>
<script type="text/js-worker">
self.onmessage = function (oEvent) {
self.postMessage(myVar);
};
</script>
<script type="text/javascript">
var oBuilder = new WebKitBlobBuilder() || new MozBlobBuilder();
Array.prototype.forEach.call(document.querySelectorAll("script[type=\"text\/js-worker\"]"), function (oScript) { oBuilder.append(oScript.textContent); });
var obj_url = window.webkitURL.createObjectURL(oBuilder.getBlob()) || window.URL.createObjectURL(oBuilder.getBlob());
document.worker = new Worker(obj_url);
document.worker.onmessage = function (oEvent) {
log_worker_msg( oEvent.data);
};
window.onload = function() { document.worker.postMessage(""); };
</script>
</head>
<body><div id="worker_log"></div></body>
</html>
答案 1 :(得分:0)
您的网络工作者支持测试包含重大错误,导致工作人员抛出参考错误。 BlobBuilder已被弃用,Blob最好在浏览器范围内使用,但它尚未得到广泛支持。
我已经纠正了嵌入式工作者测试,以便测试它的支持 XMLHttpRequest的 上传文件 的WebSockets
更正的来源在这里。 corrected source
function log_worker_msg(worker_msg) {
var frag = document.createDocumentFragment();
frag.appendChild(document.createTextNode(worker_msg));
document.querySelector("#worker_log").appendChild(frag);
}
var aFileParts = ["var xmlhttp = \'XMLHttpRequest: \' + ( ( \'function\' === typeof XMLHttpRequest ) ? \' YES\' : \' NO\' );var file_reader = 'FileReader: ' + ( ( \'function\' === typeof FileReader ) ? \' YES\' : \' NO\' );var websockets = \'Websockets: \' + ( ( \'function\' === typeof WebSocket ) ? \' YES\' : \' NO\' );var myVar = \"Test results: \" + xmlhttp + \", \" + file_reader + \", \" + websockets;self.onmessage=function(oEvent){self.postMessage(myVar)};"];
var oBuilder = new Blob(aFileParts, { "type" : "text\/javascript" });
var obj_url = window.webkitURL.createObjectURL(oBuilder) || window.URL.createObjectURL(oBuilder);
document.worker = new Worker(obj_url);
document.worker.onmessage = function (oEvent) {
log_worker_msg( oEvent.data);
};
window.onload = function() { document.worker.postMessage(""); };