网络工作者是否支持?
- XMLHttpRequest 和回调
- 上传文件
- 网络套接字
我在不同的网站上发现了非常令人困惑的答案。
谢谢你的帮助...
如有疑问,请进行测试。我的 Chromium 版本建议答案是:
来源在这里和下面。
<!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>
您的 webworker 支持测试包含重大错误,这会导致 worker 抛出参考错误。BlobBuilder 已弃用,Blob 最好在浏览器范围内使用,但尚未得到广泛支持。
为了测试它对 XMLHttpRequest 文件上传 Websockets 的支持,我已经更正了嵌入式工作者测试
更正的来源在这里。 更正的来源
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(""); };