0

I am new working with Bootstrap v4 and in the footer i see that there are 2 jQuery Scripts. One from a cdn what i understand and one local. What does this line exactly mean:

<script>window.jQuery || document.write('<script src="../../assets/js/vendor/jquery.min.js"><\/script>')</script>

Is this script loaded when the script from the cdn is not working?

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
public9nf
  • 1,311
  • 3
  • 18
  • 48
  • yes this is a local copy make sure the path is correct – madalinivascu Oct 10 '16 at 12:09
  • @zzzzBov - that answer does not match this question. This should not be marked as a duplicate. – serraosays Oct 10 '16 at 12:54
  • @staypuftman, yea, they're inverse, the answer is in the question. Certainly not ideal. There are other better duplicates I'm sure, but the point is that the information is duplicative and doesn't need to be rehashed again. – zzzzBov Oct 10 '16 at 12:55

1 Answers1

0

This double pipe (||) is the logical "or" operator. If jQuery has already been loaded on the page, window.jQuery is defined so this part of the expression evaluates to true, and the second part is not evaluated. Otherwise, the second part is evaluated, and that part actually does something: the script tag is appended to the document, causing the browser to load the resource at the given URL.

On Bootstrap's homepage (which uses Bootstrap), I can see that the line before that is:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js" integrity="sha384-THPy051/pYDQGanwU6poAc/hOdQxjnOEXzbT+OuUAFqNqFjL+4IGLBgCJC3ZOShY" crossorigin="anonymous"></script>

So, yes, this tries to fetch jQuery from the CDN, and if it fails (e.g. server unavailable), the next line ensures that jQuery is loaded from a local copy.