2

I am making a small project that will let: different websites to load my external javascript file: something like this:

<script type="text/javascript">
        var vsid = "SA89696";
        (function() { 
            var vsjs = document.createElement('script'); vsjs.type = 'text/javascript'; vsjs.async = true; vsjs.setAttribute('defer', 'defer');
            vsjs.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'example.com/robot/robot.js';
            var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(vsjs, s);
        })();
        </script>

now I want to use jquery in my javascript file, I dont have problem to load it by myself, but I dont want to cause errors to the curresnt website if it already loaded jquery.

do you have any suggestions how to load jquery with no overrides.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Tzook Bar Noy
  • 11,337
  • 14
  • 51
  • 82

1 Answers1

2

You need something like this:

<script>
if(!window.jQuery)
{
   var script = document.createElement('script');
   script.type = "text/javascript";
   script.src = "path/to/jQuery";
   document.getElementsByTagName('head')[0].appendChild(script);
}
</script>

It checks if jQuery is loaded or no..

Source

Community
  • 1
  • 1
Miro Markaravanes
  • 3,285
  • 25
  • 32