-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use a document.currentScript polyfill which works in all Internet Exp…
…lorer versions :-D (#480) * use a document.currentScript polyfill which works in all Internet Explorer versions :-D * simplify tests * modify currentScript polyfill to work in IE8
- Loading branch information
Jake Champion
committed
Feb 24, 2020
1 parent
7aa29b1
commit 653e432
Showing
6 changed files
with
46 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,7 @@ | ||
repo = "https://github.com/JamesMGreene/document.currentScript" | ||
repo = "https://github.com/amiller-gh/currentScript-polyfill" | ||
license = "MIT" | ||
docs = "https://developer.mozilla.org/en-US/docs/Web/API/Document/currentScript" | ||
spec = "https://html.spec.whatwg.org/multipage/dom.html#dom-document-currentscript" | ||
notes = [ | ||
"This polyfill will not work in IE11 because of a critical design choice made Microsoft (\"Don't Call Me IE!\") [\\[1\\]](https://msdn.microsoft.com/en-us/library/ie/bg182625.aspx)[\\[2\\]](https://msdn.microsoft.com/en-us/library/ie/dn384059.aspx)[\\[3\\]](http://www.nczonline.net/blog/2013/07/02/internet-explorer-11-dont-call-me-ie/)[\\[4\\]](http://blog.getify.com/ie11-please-bring-real-script-preloading-back/) in order to avoid consumers receiving an unnecessarily downgraded experience on websites that were making logic branch and feature decisions based on browser detection rather than feature detection." | ||
] | ||
|
||
[browsers] | ||
ie = "8 - 10" | ||
|
||
ie = "*" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,37 @@ | ||
if ((typeof WorkerGlobalScope === "undefined") && (typeof importScripts !== "function")) { | ||
(function () { | ||
|
||
var | ||
|
||
// Check if the browser supports the `readyState` property on `script` elements. | ||
// Guaranteed accurate in IE 6-10. | ||
// Not correctly supported in any other browsers. =( | ||
supportsScriptReadyState = 'readyState' in document.createElement('script'), | ||
|
||
// Unfortunately necessary browser detection for Opera. | ||
isOpera = self.opera && self.opera.toString() === '[object Opera]', | ||
|
||
// Has support for `Object.defineProperty`. | ||
// Even IE8's incomplete implementation is sufficient here since it works on | ||
// native DOM interfaces like `document`. | ||
canDefineProp = typeof Object.defineProperty === 'function', | ||
|
||
// Get the currently "executing" (i.e. EVALUATING) `script` DOM element per the | ||
// spec requirements for `document.currentScript`. | ||
_currentEvaluatingScript = function () { | ||
var | ||
|
||
// Live NodeList collection | ||
scripts = document.getElementsByTagName('script'); | ||
|
||
// Guaranteed accurate for IE 6-10 (but NOT IE11!). | ||
for (var i = scripts.length; scripts[--i];) { | ||
if (scripts[i].readyState === 'interactive') { | ||
return scripts[i]; | ||
} | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
|
||
if (!supportsScriptReadyState) { | ||
throw new Error('Cannot polyfill `document.currentScript` as your browser does not support the "readyState" DOM property of script elements. Please see https://github.com/Financial-Times/polyfill-service/issues/952 for more information.'); | ||
} | ||
if (isOpera) { | ||
throw new Error('Cannot polyfill `document.currentScript` as your Opera browser does not correctly support the "readyState" DOM property of script elements. Please see https://github.com/Financial-Times/polyfill-service/issues/952 for more information.'); | ||
} | ||
if (!canDefineProp) { | ||
throw new Error('Cannot polyfill `document.currentScript` as your browser does not support `Object.defineProperty`. Please see https://github.com/Financial-Times/polyfill-service/issues/952 for more information.'); | ||
} | ||
|
||
|
||
Object.defineProperty(document, 'currentScript', { | ||
get: function Document$currentScript() { | ||
return _currentEvaluatingScript(); | ||
}, | ||
configurable: true | ||
}); | ||
|
||
}()); | ||
} | ||
// document.currentScript polyfill by Adam Miller -- modifed by Jake Champion | ||
|
||
// MIT license | ||
|
||
(function(document){ | ||
var currentScript = "currentScript", | ||
scripts = document.getElementsByTagName('script'); // Live NodeList collection | ||
|
||
// If browser needs currentScript polyfill, add get currentScript() to the document object | ||
if (!(currentScript in document)) { | ||
Object.defineProperty(document, currentScript, { | ||
get: function(){ | ||
|
||
// IE 6-10 supports script readyState | ||
// IE 10+ support stack trace | ||
try { throw new Error(); } | ||
catch (err) { | ||
|
||
// Find the second match for the "at" string to get file src url from stack. | ||
// Specifically works with the format of stack traces in IE. | ||
var i = 0; | ||
var res = ((/.*at [^(]*\((.*):.+:.+\)$/ig).exec(err.stack) || [false])[1]; | ||
|
||
// For all scripts on the page, if src matches or if ready state is interactive, return the script tag | ||
for(i = 0; i < scripts.length; i++){ | ||
if(scripts[i].src == res || scripts[i].readyState == "interactive"){ | ||
return scripts[i]; | ||
} | ||
} | ||
|
||
// If no match, return null | ||
return null; | ||
} | ||
} | ||
}); | ||
} | ||
})(document); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters