jsdom
을 통해 페이지를 실행할 때 페이지 스크립트의 $(document).ready
블록이 실행되지 않는 것 같습니다.
<html>
<body>
If everything works, you should see a message here: <h2 id="msg"></h2>
<script>
var checkpoint1 = true
var checkpoint2 = false
$(document).ready(function(){
checkpoint2 = true
$('#msg').html("It works, it works, it works!")
})
</script>
</body>
</html>
과 codez :
fs = require('fs');
htmlSource = fs.readFileSync("public/examples/test_js_dom.html", "utf8");
global.jsdom = require("jsdom");
jsdom.defaultDocumentFeatures = {
FetchExternalResources : ['script'],
ProcessExternalResources : ['script'],
MutationEvents : '2.0',
QuerySelector : false
};
doc = jsdom.jsdom(htmlSource)
window = doc.createWindow()
jsdom.jQueryify(window, "http://code.jquery.com/jquery-1.8.3.min.js", function(){
console.log(window.checkpoint1);
console.log(window.checkpoint2);
console.log(window.$().jquery)
console.log("body:");
console.log(window.$('body').html());
});
출력 :
[email protected]:~/projects/notjs$ test/jsdom.js
true
false
1.8.3
body:
If everything works, you should see a message here: <h2 id="msg"></h2>
<script>
var checkpoint1 = true
var checkpoint2 = false
$(document).ready(function(){
checkpoint2 = true
$('#msg').html("It works, it works, it works!")
})
</script>
<script class="jsdom" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
내가 잘못 뭐하는 거지 여기
는 HTML입니까?만족스럽지 못한 스택 오버 플로우 율에 세부 사항 추가.
[email protected]:~/projects/notjs$ npm ls jsdom
[email protected] /Users/Bee/projects/notjs
├─┬ [email protected]
│ └── [email protected]
└── [email protected]
[email protected]:~/projects/notjs$ node -v
v0.8.15
[email protected]:~/projects/notjs$ npm -v
1.1.66
[해결] :
정답에 저를 선도하는 당신에게 데이브 감사드립니다.나는 완전한 jsdom 대답이 이것 같이 간다 고 생각한다; jsdom.jQuerify를 사용하지 말고, 페이지 스크립트 위 페이지에서 jQuery를로드하는 script 태그를 추가하십시오 (브라우저에서 페이지를로드하기 위해 필요함).
HTML :
...
If everything works, you should see a message here: <h2 id="msg"></h2>
<script src="http://notjs.org/vendor/jquery-1.8.3.min.js"></script>
<script>
var checkpoint1 = true
var checkpoint2 = false
$(document).ready(function(){
var checkpoint2 = true
...
코드 :
...
doc = jsdom.jsdom(htmlSource)
window = doc.createWindow()
window.addEventListener('load', function(){
console.log(window.checkpoint1);
console.log(window.checkpoint2);
console.log(window.$().jquery)
console.log("body:");
console.log(window.$('body').html());
});
...
Duh! 예, 정답은 페이지 스크립트에 jquery를로드하기 위해 스크립트 태그를 페이지에 추가하는 것입니다. 감사! –