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 || 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 Zea Aug 15 '13 at 2:24
1  
Giving an upvote is enough to show thanks on Stack Exchange (a comment is okay as well, as long as it's still substantial). See this for more info. –  Jamal May 7 at 17:45
    
Please do not adjust your question in response to answers as it invalidates the answer. –  Hosch250 May 7 at 17:47
    
@Hosch250 No one is adjusting the question, another moderator edit the title to be more descriptive. Answers are still 100% valid. –  Ricardo Zea Jun 10 at 21:06

1 Answer 1

up vote 3 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 Zea Aug 15 '13 at 2:22

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.