[편집 : 입니다. 아래 편집 참조]. jQuery가 이미 정의되어 있는지 확인하고 그렇지 않은 경우 스크립트를 포함하십시오.
var scripts = [], undef;
if(typeof window.jQuery === typeof undef){
scripts = ["//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js", "../javascripts/fixit.js"];
}
jsdom.env(
data,
scripts,
function (errors, window) {
⋮
나는 그것을 테스트하지 않은,하지만 config.scripts
가 빈 배열 인 경우 jsdom source code에 의해 판단하는 것이 scriptComplete 콜백을 불 수 있습니다.
EDIT : 귀하의 의견을 읽은 후에 나는 그것이 효과가 없을 것임을 깨달았습니다. jQuery
을 확인하는 컨텍스트는 jsdom.env
의 window
개체의 범위를 벗어납니다. jsdom은 유스 케이스가 임시 DOM에서 메모리 내 작업 일 가능성이 있으므로 추가 할 스크립트가 있는지 확인하지 않습니다.
수정 된 DOM을 유지하고 파일에서 다시 읽을 수 있고 이러한 중복 된 <scipt />
요소를 추가하지 않고 다시 처리 할 수있는 경우 가장 쉬운 해결책은 파일을 작성하기 직전에이를 제거하는 것입니다.
/* if you don't want to save the document with <script> elements at all */
Array.prototype.slice.call(document.scripts) // convert document.scripts to regular Array
.forEach(
function(script){
/* you probably want to check if script.src matches jQuery or whatever,
* e.g. if(script.src.indexOf('jquery.min.js' > -1){
*/
script.parentNode.removeChild(script);
});
/* alternatively if you want to have each <script> exactly one (remove duplicates) */
var srcs = {};
Array.prototype.slice.call(document.scripts) // convert document.scripts to regular Array
.forEach(
function(script){
if(srcs[ scipt.src ]){
script.parentNode.removeChild(script);
}
else {
srcs[ script.src ] = 1;
}
});
이 행하기 전에 위의 중 하나를 추가 : 코드의 방법 $("head").append('<link ...
으로
var output = window.document.doctype + window.document.innerHTML;
이이 링크를 여러 번 추가합니다. 당신은 그것의 존재를 확인해야합니다
이
if($('link[src$="stylesheets/test.css"]').length == 0) $("head").append('<link ...
호기심에서 벗어남 : 외부에서 jsdom (data)의'window' 객체에 액세스하거나 콜백 함수에서만 액세스 할 수 있습니까? – pawel
'document.parentWindow'는 찾고있는 표준 DOM API입니다. – Domenic
그럼'if (typeof document.parentWindow.jQuery == "undefined")'도 잘 작동 할 수 있습니다. 맞습니까? – pawel