They might come in handy when you think of loading scripts lazily. Also, I remember I once used an ID on a script tag to facilitate the changes on meta content, per state change (it was a single page application).
With an ID on a script tag, you can check if on a particular instance, whether a script tag with a particular ID is there in the DOM or not. If it's not there, and if your route requires it to be there, you can load it then (given on certain conditions).
Suppose there's a script that you need to load lazily. Let's take an example of some chat widget. Cases can arise that may tend you to load the chat widget only and only whenever a user happens to click on, say, a chat button.
Here, you'll be lazily loading the chat. So, a possible case out of it can be:
"if user clicks on chat button, inject the chat widget js into the DOM".
Simple? Hell yes. But, think about the case when a user clicks on the chat button again. What will happen? The same script would be injected again, but you certainly don't want that to happen, do you?
So, let's say the chat widget js goes into the DOM by:
<script id="chatWidgetScript" src="...whatever..."></script>
A good solution to tackle the "second click" problem on the chat button will be to see if the script already exists in the DOM, and if it does, don't let the same get appended twice into the DOM.
So, your button could be:
<button onclick="loadChatWidget()"> Chat! </button>
And your loadChatWidget() function could be:
loadChatWidget(){
if(document.getElementById("chatWidgetScript")){
//chat script is already there in the DOM, no need to load it again.
return;
}
/*
if document.getElementById("chatWidgetScript") returns null, this would
mean that the chat widget isn't there in the DOM, and you might want to
load the same now.
*/
// LOAD SCRIPT HERE
}
This is just one way of using an ID on a script tag, and it is useful too!