Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Is there a way to optimize these four lines into something more efficient?

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/jquery-1.10.2.min.js"><\/script>')</script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<script>window.jQuery.ui || document.write('<script src="js/jquery-ui-1.10.3.custom.min.js"><\/script>')</script>

Thanks in advance,

share|improve this question
1  
Keep in mind that script tags loaded with document.write are loaded asynchonously and won't block execution of the rest of your javascript code. See this article for a good description of the problem and solution: css-tricks.com/snippets/jquery/load-jquery-only-if-not-present –  cfs Aug 14 '13 at 18:55
 
@cfs Yes, I understand about asynchronously loaded scripts, but I didn't know that scripts loaded with document.write actually were loaded this way. Thanks a lot for the info. Gave you an upvote. –  Ricardo Aug 15 '13 at 2:24
add comment

1 Answer

up vote 2 down vote accepted

I think this is the best approach possible, but looks like you have a bug in the 4th statement, it should be

<script>window.jQuery.ui || document.write('<script src="js/jquery-ui-1.10.3.custom.min.js"><\/script>')</script>

because here you want to check whether the ui library is included

share|improve this answer
 
Ah! I hadn't noticed the error window.jQuery, it needs to be window.jQuery.ui. Error corrected in the question. Thanks for the help Arun. –  Ricardo Aug 15 '13 at 2:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.