在我的應用程序,我打開一個新窗口var w = window.open()
。我訪問CanvasJS API與:如何從新窗口訪問jQuery?
var canvas = w.document.createElement('script');
canvas.type = "text/javascript";
canvas.src = "https://canvasjs.com/assets/script/canvasjs.min.js";
w.document.head.appendChild(canvas);
這工作得很好。我與jQuery的同樣的事情,並確保我自己的腳本之前將其追加但我得到這個錯誤:ReferenceError: $ is not defined
這裏是我的代碼如下所示:
var w = window.open('','_blank',width,height);
w.document.body.innerHTML = '<body> //create chart container here </body>';
var jQuery = w.document.createElement('script');
var canvas = w.document.createElement('script');
var script = w.document.createElement('script');
canvas.type = "text/javascript";
canvas.src = "https://canvasjs.com/assets/script/canvasjs.min.js";
jQuery.type = "text/javascript";
jQuery.src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js";
script.textContent = "//my script content here, this is where I use the '$' jQuery object";
w.document.head.appendChild(jQuery);
w.document.head.appendChild(canvas);
w.document.head.appendChild(script);
動態添加的腳本異步加載,並且完成的加載順序不同。 – Teemu