javascript - Verify External Script Is Loaded -


i'm creating jquery plugin , want verify external script loaded. internal web app , can keep script name/location consistent(mysscript.js). ajaxy plugin can called on many times on page.

if can verify script not loaded i'll load using:

jquery.getscript() 

how can verify script loaded because don't want same script loaded on page more once? shouldn't need worry due caching of script?

update: may not have control on uses plugin in our organization , may not able enforce script not on page or without specific id, script name in same place same name. i'm hoping can use name of script verify it's loaded.

if script creates variables or functions in global space can check existance:

external js (in global scope) --

var mycustomflag = true; 

and check if has run:

if (typeof window.mycustomflag == 'undefined') {     //the flag not found, code has not run     $.getscript('<external js>'); } 

update

you can check existence of <script> tag in question selecting of <script> elements , checking src attributes:

//get number of `<script>` elements have correct `src` attribute var len = $('script').filter(function () {     return ($(this).attr('src') == '<external js>'); }).length;  //if there no scripts match, load if (len === 0) {     $.getscript('<external js>'); } 

or can bake .filter() functionality right selector:

var len = $('script[src="<external js>"]').length; 

Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -