Web worker支持吗?
- XMLHttpRequest和回调函数
- 文件上传
- Websockets
我在不同的网站上找到了非常混乱的答案。
感谢您的帮助...
Web worker支持吗?
我在不同的网站上找到了非常混乱的答案。
感谢您的帮助...
当你有疑问时,请进行测试。我使用的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支持测试存在重大错误,导致工作线程抛出引用错误。 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(""); };