├── .editorconfig ├── LICENSE ├── README.md ├── currentScript.js └── package.json /.editorconfig: -------------------------------------------------------------------------------- 1 | [*] 2 | indent_style = space 3 | indent_size = 2 -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Adam Miller 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # document.currentScript Polyfill 2 | 3 | #### An exceptionally slim (~310b minified) polyfill for document.currentScript in IE9-IE11 4 | 5 | This polyfill may not adhere strictly to spec when called in async code or in a callback. In these situations the spec calls for document.currentScript to return null. However, for the grand majority of your document.currentScript needs, this polyfill will do the job! 6 | -------------------------------------------------------------------------------- /currentScript.js: -------------------------------------------------------------------------------- 1 | // document.currentScript polyfill by Adam Miller 2 | 3 | // MIT license 4 | 5 | (function (document) { 6 | var currentScript = 'currentScript'; 7 | 8 | // If browser needs currentScript polyfill, add get currentScript() to the document object 9 | if (!(currentScript in document)) { 10 | Object.defineProperty(document, currentScript, { 11 | get: function () { 12 | // IE 8-10 support script readyState 13 | // IE 11+ support stack trace 14 | try { 15 | throw new Error(); 16 | } 17 | catch (err) { 18 | // Find the second match for the "at" string to get file src url from stack. 19 | // Specifically works with the format of stack traces in IE. 20 | var i = 0, 21 | stackDetails = (/.*at [^(]*\((.*):(.+):(.+)\)$/ig).exec(err.stack), 22 | scriptLocation = (stackDetails && stackDetails[1]) || false, 23 | line = (stackDetails && stackDetails[2]) || false, 24 | currentLocation = document.location.href.replace(document.location.hash, ''), 25 | pageSource, 26 | inlineScriptSourceRegExp, 27 | inlineScriptSource, 28 | scripts = document.getElementsByTagName('script'); // Live NodeList collection 29 | 30 | if (scriptLocation === currentLocation) { 31 | pageSource = document.documentElement.outerHTML; 32 | inlineScriptSourceRegExp = new RegExp('(?:[^\\n]+?\\n){0,' + (line - 2) + '}[^<]*