0

Possible Duplicate:
How may I reference the script tag that loaded the currently-executing script?
Can you select the script element that included the JavaScript?

Is there any identifyScript that could make this work? jQuery is cool too.:

<script type="text/javascript" id="script1">
    function identifyScript(){...}
</script>
<script type="text/javascript" id="script1">
    identifyScript(); //This function would alert "script1"
</script>
<script type="text/javascript" id="script2">
    identifyScript(); //This function would alert "script2"
</script>
Community
  • 1
  • 1
A F
  • 7,424
  • 8
  • 40
  • 52
  • No, that isn't possible directly, though you could use document.write to create an element right after the script that you can target from within the script, as long as it gets ran during the initial page rendering. – Kevin B Oct 18 '12 at 15:34
  • 1
    *Why* do you need to do this? Perhaps there is a more direct way of acomplish your objective. – hugomg Oct 18 '12 at 15:34
  • @missingno, this is one of those features that would just be nice to have. Plugin configuration could move to attributes on the including script. – zzzzBov Oct 18 '12 at 15:36
  • @zzzzBov the nail on the head. It would be easy to hand some code off to a client with `` – A F Oct 18 '12 at 15:38
  • @zzzzBov, yes its a duplicate. How do I close? – A F Oct 18 '12 at 15:38
  • @AakilFernandes, the message was automatically generated when I voted to close the question. When there's enough votes it'll automatically be closed with links pointing to the original questions. There's nothing that you need to do. – zzzzBov Oct 18 '12 at 15:39

2 Answers2

5

As long as it is guaranteed that the script files are getting loaded sequentially (which means, not asyncronously), you can call the following code within each javascript file:

var scripts        = document.getElementsByTagName( 'script' ),
    lastTag        = scripts[ scripts.length - 1 ];

alert( lastTag.id );

However, this concept breaks as soon as there is an async or defer attribute within those <script> elements. Each of these attributes allows the browser to delay the loading of scripts and order is no longer guaranteed.

But if you're loading your files without any async flags, this will work just fine because each script accesses the last inserted <script> node, which must be the one it was loaded from.

jAndy
  • 231,737
  • 57
  • 305
  • 359
0
<script type="text/javascript">
    function identifyScript() {
        scripts = document.getElementsByTagName("script");
        alert(scripts[scripts.length - 1].id);
    }
</script>
<script type="text/javascript" id="script1">
    identifyScript(); //This function would alert "script1"
</script>
<script type="text/javascript" id="script2">
    identifyScript(); //This function would alert "script2"
</script>

This works because at the time it's run in each block, that block is the last one to exist so is the last one in the collection.

limos
  • 1,526
  • 12
  • 13