0

I found this great piece of code here

So if you're trying to include jQuery via Google or the jQuery website and they fail, your own copy is included on your page.

 <script>window.jQuery || document.write('<script src="path/to/your/jquery"><\/script>')</script>

I'm wondering is it possible to do this with other libraries?

Community
  • 1
  • 1
Daft
  • 10,277
  • 15
  • 63
  • 105

1 Answers1

1

It would depend upon the library that you are asking about. But to give you an idea of how it might be possible. Let's just say you have your own Javascript library and it contains an object called, fooBar (of global scope) for example. In that case, you may want to use the following:

<script>window.fooBar || document.write('<script src="path/to/your/foobar"><\/script>')</script>

Plus what makes you think that above code is jquery specific, it isn't. It is just Javascript being used in a clever way. What || does is try to execute the left side of it, if it fails it jumps to the right side of it otherwise it ignores it.

For further detail, I would recommend this article

Kamran Ahmed
  • 11,809
  • 23
  • 69
  • 101