Reputation: 9855
Not sure if this is the correct place to post, But looking at the HTML5 boilerplate I have...
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.js"></script>
<script>window.jQuery || document.write('<script src="_includes/js/libs/jquery-1.6.2.min.js"><\/script>')</script>
Now I've read the second script is a fall back for if the first is down, but how? I always thought || was an OR operator and to me it just looks like it says when the page loads write an old jQuery lib to the page?
Upvotes: 0
Views: 330
Reputation: 339816
The second half of the ||
operator is only evaluated if the left hand side is "falsey". This is known as "short circuit evaluation" *
So when window.jQuery
is undefined
, it does the document.write()
.
* true || x == true
, regardless of x's value, so a short circuit ||
operator given true
for its left hand operand will skip evaluating the right hand of the expression since it doesn't change the result.
Conversely false && x == false
.
Upvotes: 4
Reputation: 337560
Because javascript is a falsy language, if jQuery is not available window.jQuery
will return undefined
which equates to false
.
Therefore, the second part of the statement will be executed, which writes the <script>
tag to the document containing the local backup version of jQuery.
Upvotes: 1