From 8d33f90c1363fa8404da80aef446f9bdb6cae90c Mon Sep 17 00:00:00 2001 From: Ed Sanders Date: Mon, 1 Dec 2014 13:19:06 +0000 Subject: [PATCH] Check body exists before attaching load listener & rename flag to LOADED_SYNC --- papaparse.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/papaparse.js b/papaparse.js index 7832d24..ab50e66 100644 --- a/papaparse.js +++ b/papaparse.js @@ -7,7 +7,7 @@ { "use strict"; - var IS_WORKER = !global.document, IS_SYNC = false, AUTO_SCRIPT_PATH; + var IS_WORKER = !global.document, LOADED_SYNC = false, AUTO_SCRIPT_PATH; var workers = {}, workerIdCounter = 0; // A configuration object from which to draw default settings @@ -156,9 +156,17 @@ { AUTO_SCRIPT_PATH = getScriptPath(); // Check if the script was loaded synchronously - document.body.addEventListener('load', function () { - IS_SYNC = true; - }, true); + if ( document.body ) + { + document.body.addEventListener('load', function () { + LOADED_SYNC = true; + }, true); + } + else + { + // Body doesn't exist yet, must be synchronous + LOADED_SYNC = true; + } } @@ -1367,7 +1375,7 @@ { if (!Papa.WORKERS_SUPPORTED) return false; - if (!IS_SYNC && Papa.SCRIPT_PATH === null) + if (!LOADED_SYNC && Papa.SCRIPT_PATH === null) throw new Error( 'Script path cannot be determined automatically when Papa Parse is loaded asynchronously. ' + 'You need to set Papa.SCRIPT_PATH manually.'