John John - 1 year ago 121
Node.js Question

JSDOM not loading script files in Node

I am trying to do a test of loading up an html page in jsdom which will eventually generate graphs. I cannot overcome the first hurdle of just loading the html page and having the javascript execute.

Below is my html page which I am trying to load which doesnt take any parameters and just renders a simple graph.

<script src=""/>
<script src=""/>
<script src=""/>
<script src=""/>
var testVar = true;
function test(){
testVar = false;
FusionCharts.ready(function () {
var revenueChart = new FusionCharts({
type: 'column2d',
renderAt: 'container',
width: '400',
height: '200',
dataFormat: 'json',
dataSource: {
"chart": {
"caption": "Split of Revenue by Product Categories",
"subCaption": "2014",
"numberPrefix": "$",
"theme": "fint",
"captionFontSize": "13",
"subcaptionFontSize": "12",
"subcaptionFontBold": "0",
"showValues": "0"
"data": [{
"label": "Food",
"value": "28504"
}, {
"label": "Apparels",
"value": "14633"
}, {
"label": "Electronics",
"value": "10507"
}, {
"label": "Household",
"value": "4910"

var svg = $('#container').html();
<div id="container">Charts will render here</div>

Here is the code in node where I am trying to load this page..

var config = {
file: path.join(__dirname, "chart.html"),
FetchExternalResources: ["script"],
ProcessExternalResources: ["script"],
MutationEvents: '2.0'
onload: function(err, window) {
created: function(err, window) {
done: function(err, window) {
console.log('*****got err ' + err.message);

global.window = window;
console.log('inside done ----------')
var $ = window.jQuery || window.$,
FusionCharts = window.FusionCharts,
document = window.document;

if(typeof FusionCharts == 'undefined')
console.log('FusionCharts NOT LOADED')
console.log('FusionCharts LOADED')

if(typeof $ == 'undefined')
console.log('JQUERY NOT LOADED')
console.log('JQUERY LOADED')

console.log('testVar ' + window.testVar)
console.log('testVar ' + window.testVar)
console.log('svg is ' + window.svg);



The strange thing here is that if i do not include
in the config object, it will not load them and make them available in the
callback even thought it is there on the html page.

is never defined in the callback even thought it is present in the page, the same with
, even though its in the html page it just does not seem to be available in the callback.

I have tried all different variations of creating the jsdom object, but none of them allows the page to load the scripts rather than me passing it into the config object, and none of the different versions allow me to access variables and functions defined in the script tags.

Is there something I am missing ?

Answer Source

Change your script elements so that they are correct HTML:

<script src=""></script>

If you do that everything will load fine and you won't have to use scripts in your configuration.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download