Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
-1
rated 0 times [  6] [ 7]  / answers: 1 / hits: 18004  / 8 Years ago, thu, august 4, 2016, 12:00:00

Browser is Chrome, document.currentScript should be supported but



index.html



<link href=css/main.css rel=stylesheet />
<script src=1.js></script>
<style>


1.js



setInterval(function() {
var fullUrl = document.currentScript.src;
console.log(fullUrl)
},2000)


Error : 
1.js:4 Uncaught TypeError: Cannot read property 'src' of null

More From » javascript

 Answers
32

document.currentScript only returns the script that is currently being processed. During callbacks and events, the script has finished being processed and document.currentScript will be null. This is intentional, as keeping the reference alive would prevent the script from being garbage collected if it's removed from the DOM and all other references removed.



If you need to keep a reference to the script outside of any callbacks, you can:



var thisScript = document.currentScript;

setInterval(() => console.log(thisScript.src), 2000);

[#61143] Tuesday, August 2, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
brodyfrancisi

Total Points: 1
Total Questions: 102
Total Answers: 89

Location: Marshall Islands
Member since Mon, May 31, 2021
3 Years ago
brodyfrancisi questions
;