From b8f2cb59e10ce74bab105e45934ee2fa89846e7d Mon Sep 17 00:00:00 2001 From: dtracers Date: Mon, 3 Jul 2017 19:47:03 -0500 Subject: [PATCH] Gets the correct address for the current script The old way breaks if you load the script using html imports Then the url is pointing at an invalid script. --- lib/jailed.js | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/jailed.js b/lib/jailed.js index 4eff77a..6e407be 100644 --- a/lib/jailed.js +++ b/lib/jailed.js @@ -29,12 +29,22 @@ if (__is__node__) { __jailed__path__ = __dirname + '/'; } else { // web - var scripts = document.getElementsByTagName('script'); - __jailed__path__ = scripts[scripts.length-1].src - .split('?')[0] - .split('/') - .slice(0, -1) - .join('/')+'/'; + if (doucment.currentScript) { + var script = document.currentScript; + __jailed__path__ = script.src + .split('?')[0] + .split('/') + .slice(0, -1) + .join('/')+'/'; + } else { + // for legacy browsers and IE + var scripts = document.getElementsByTagName('script'); + __jailed__path__ = scripts[scripts.length-1].src + .split('?')[0] + .split('/') + .slice(0, -1) + .join('/')+'/'; + } }