\ [JavaScript]WebWorker 내부의 JQuery 사용 :: Something New
728x90
반응형
  1. 웹 작업자는 DOM 요소와 상호 작용할 수 없음(DOM은 브라우저의 주 스레드에 의해 생성되기 때문) 
    C# 또는 Vb.NET 사용하여 프로그래밍을 하는 경우 해당
  2. JQuery는 웹 작업자의 JS 파일 내부에로드할 수 없습니다.
    JQuery가 DOM이 존재할 것으로 예상하고 WebWorker가 존재하지 않기 때문(WebWorker 내부에 스크립트를 로드)

$.ajax ()를 사용한 경우라면 JQuery로 작성된 부분을 JavaScript으로 바꿔주거나 가짜 DOM을 추가해주는 방법을 선택

function loadFakeDOMforJQuery()
{
var document = self.document = {parentNode: null, nodeType: 9, toString: function() 
{return "FakeDocument"}};
var window = self.window = self;
var fakeElement = Object.create(document);
fakeElement.nodeType = 1;
fakeElement.toString=function() {return "FakeElement"};
fakeElement.parentNode = fakeElement.firstChild = fakeElement.lastChild = fakeElement;
fakeElement.ownerDocument = document;

document.head = document.body = fakeElement;
document.ownerDocument = document.documentElement = document;
document.getElementById = document.createElement = function() {return fakeElement;};
document.createDocumentFragment = function() {return this;};
document.getElementsByTagName = document.getElementsByClassName = function()
{return [fakeElement];};

document.getAttribute = document.setAttribute = document.removeChild = 
document.addEventListener = document.removeEventListener = 
function() {return null;};
document.cloneNode = document.appendChild = function() {return this;};
document.appendChild = function(child) {return child;};
console.log("loaded FAKE DOM for JQuery");
}

JQuery 파일을 로드하기 전에 가짜 DOM을 만들고 WebWorker내부에

함수를 정의한 후 아래함수를 사용하여 JQuery를 로드

loadFakeDOMforJQuery();
importScripts("/jquery-1.10.2.min.js");

 

Use JQuery inside HTML5 Web Worker – My Share'Points – And other stuff (wordpress.com)https://stackoverflow.com/questions/10491448/how-to-access-jquery-in-html-5-web-workerhttps://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers

 

Use JQuery inside HTML5 Web Worker

All, Web workers are game changers in terms of how they can offload work from the browser’s main thread. Given that the current trend is about implementing a page which is dynamic in nature, …

anandabandaru.wordpress.com

 

728x90

+ Recent posts