Search code examples
javascriptjquerydomgetscript

$.getScript, possible to give included script an id?


Via jquery's $.getScript method, can you give the included script a DOM id?

So generated code should be:

<script type="text/javascript" id="xxxxxx" src="..."></script>

I know I could probably just document.write that line myself, but $.getScript must be there for a reason right? (cross browser compatibility, etc?)


Solution

  • I think this has maybe been answered/discussed before here: Why call $.getScript instead of using the <script> tag directly?.

    getScript allows you to dynamically load a script in situations where it's either desirable to delay the loading of the script, in situations where you want to get a status callback on when the script has been loaded or in situations where you couldn't use a script tag.

    getScript has some downsides in that it's subject to the same-origin policy whereas a script tag is not.

    If have seen other web pages put an ID on a script tag (smugmug.com), but I've also seen that flagged as non-standard when testing standard's compliance. It seems to work and be used by others, but I'm guessing it isn't standard.