mare
mare

Reputation: 13083

conditional loading of jquery and jquery UI issues

The following works perfectly:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script>!window.jQuery && document.write(unescape('%3Cscript src="/scripts/jquery-1.6.2.min.js"%3E%3C/script%3E'))</script>

However, doing the same for Jquery UI (but with the change to window.ui - this should be right but I'm not 100% sure) does not work - actually it loads the local version every time not just when CDN file is not available (per Firebug):

<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-ui.min.js"></script>
<script>!window.ui && document.write(unescape('%3Cscript src="/scripts/jquery-ui-1.8.14.min.js"%3E%3C/script%3E'))</script>

It does load the local version when network is unavailable but also when the CDN version is loaded (which means unnecessary double loading).

?

Upvotes: 2

Views: 2348

Answers (2)

zmonteca
zmonteca

Reputation: 2354

This might be stupid, but I prefer OR logic over AND for this:

<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-ui.min.js"></script>
<script>window.jQuery.ui || document.write('<script src="/scripts/jquery-ui-1.8.14.min.js"<\/script>'))</script>

Upvotes: 3

Nick Craver
Nick Craver

Reputation: 630607

Your load check just needs a little tweak. jQuery UI creates a ui object under the jQuery (or $) object, not under window. So window.ui should be window.jQuery.ui, like this:

<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-ui.min.js"></script>
<script>!window.jQuery.ui && document.write(unescape('%3Cscript src="/scripts/jquery-ui-1.8.14.min.js"%3E%3C/script%3E'))</script>

Upvotes: 4

Related Questions