16

Does somebody know if its possible to select the current script-tag with jQuery without any other selectordefinitions?

<script type="text/javascript">
$(document).ready( function(){
    // Here i need to select the current tag "<script ..."
})
</script>
AppGeer
  • 725
  • 1
  • 11
  • 27
  • possible duplicate of [How may I reference the script tag that loaded the currently-executing script?](http://stackoverflow.com/questions/403967/how-may-i-reference-the-script-tag-that-loaded-the-currently-executing-script) – Flimm Jun 30 '15 at 15:53

5 Answers5

9

Outside the document ready method, just do $('script').last();:

<script type="text/javascript">
var currentScript = $('script').last();
$(document).ready( function(){
    //Use the variable currentScript here
})
</script>

Or simply give an id to your script.

Karl-André Gagnon
  • 33,662
  • 5
  • 50
  • 75
7

The most robust way to achieve this is:

<script>
 (function(script){
    //do whatever you want with script here...
 })(document.currentScript);
</script>

It is more robust than other solutions because you may also use it after the document has loaded. Also it does not require the definition of a variable (if you have several script tags this might be an issue since you cannot reuse the same variable name). With JQuery:

<script>
     (function(script){
        $(document).ready(function(){
            // do whatever you want after the DOM as loaded here...
        });
     })(document.currentScript);
</script>
Vincent Pazeller
  • 1,448
  • 18
  • 28
5

I think this is fastest way

<script type="text/javascript">
    var scripts = document.getElementsByTagName("script");
    var thisScript = scripts[scripts.length - 1];
</script>
  • 2
    I like the non-jQuery approach – jasonscript Mar 31 '15 at 04:29
  • 1
    I presume this won't work if the original script tag had the `defer` or `async` attributes set. – Flimm Jun 30 '15 at 15:52
  • 2
    What if there are multiple script tags on the page? This doesn't select the script tag that surrounds the current javascript. It just selects the last one on the page. – Jake Wilson Dec 08 '15 at 19:02
  • 2
    @JakeWilson As commented on Karl-André Gagnon's similar answer, when the browser encounters this script tag, it is the last one on the page (as long as `defer` or `async` aren't set). – Teepeemm Aug 08 '19 at 02:34
2
<script type="text/javascript">
  var currentScript = document.currentScript || (function() {
     var scripts = document.getElementsByTagName('script');
     return scripts[scripts.length - 1];
  })();
<script/>

document.currentScript works on most browsers and scripts[scripts.length - 1] is the fallback for the other ones (wich may impose some restrictions to <script async>).

Further discussion

Cléssio Mendes
  • 996
  • 1
  • 9
  • 25
-1

try this

var scripts = document.getElementsByTagName("script");
var thisScript = scripts[scripts.length - 1];
Lugarini
  • 792
  • 5
  • 11
  • 32
  • 2
    This returns the `type` attribute of *all* script tags. – Flimm Jun 30 '15 at 15:52
  • The previous comment refers to a prior version of this answer. The current answer is now similar to several other answers on this page. – Teepeemm Aug 08 '19 at 02:36