1

I'm working on some possible add-on modules for an open-source project, written in php. I'm puzzled by the code I'm finding in the html header, but I'm reasonably new to working with anything other than html and css.

<script type="text/javascript">window.jQuery || document.write(unescape('%3Cscript type="text/javascript" src="//code.jquery.com/jquery-1.12.0.min.js"%3E%3C/script%3E'));</script>
<script type="text/javascript">window.jQuery || document.write(unescape('%3Cscript type="text/javascript" src="<?php echo $template->get_template_dir('.js',DIR_WS_TEMPLATE, $current_page_base,'jscript'); ?>/jquery.min.js"%3E%3C/script%3E'));</script>

The closest answer I found to why they are adding it twice is that the local copy is a fallback in case the CDN is overloaded. Force Download from local cache

But even in that case, wouldn't the main, top link be:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"> </script>

... and then if anything was to be included via document.write(), it would be the fallback?

It's also throwing up error warnings in the developer console. I'm asking if there is some sort of reason (that I wouldn't understand yet) for doing it the way they're doing it. If I replace the two lines of document.write() stuff with the direct link, I don't get console warnings, and my project seems to load much faster.

Community
  • 1
  • 1
suedfinym
  • 13
  • 4
  • 3
    Yes, and you've given said reason. but typically the fallback url is to a local script. Using that method to include the primary can be a minor performance hit on page load, but otherwise no harm. – Kevin B Apr 28 '17 at 15:10
  • Your also including a newer version of jQuery probably dispelling the normal errors. – li x Apr 28 '17 at 15:12
  • The second one should NOT be a link tag or you load the library twice. http://stackoverflow.com/questions/1014203/best-way-to-use-googles-hosted-jquery-but-fall-back-to-my-hosted-library-on-go – epascarello Apr 28 '17 at 15:12
  • Ok, thanks. The second link IS to a local copy (which also turns out to be jQuery v1.12.0 ). But, what y'all are saying is that even if it is to be included as a fallback, instead of making the fallback a link tag, it should check to see if the CDN library downloaded before using it-- and the local version should be updated? – suedfinym Apr 28 '17 at 15:43

1 Answers1

1

The reason document.write is used to load the script tag is because of the window.jQuery || bit just beforehand. Using the || operator like this checks to see if the left-hand expression is defined - if so, it does nothing; if not, it runs the expression on the right-hand side, which creates the script tag. This creates the ability to load jQuery only if it hasn't been loaded on the page already.

Hydrothermal
  • 4,851
  • 7
  • 26
  • 45
  • Ah, ok, I think I get it now. So - `window.jQuery ||` is doing the same thing as checking with `if (!window.jQuery)` , and furthermore, the top link is checking to make sure it wasn't already downloaded from Google or Microsoft (as discussed here: [link](http://stackoverflow.com/a/9400432/7565134) ) – suedfinym May 03 '17 at 16:16
  • @suedfinym Yep, exactly. If jQuery is already loaded then both conditions fail; if it's not loaded at first then the first one passes and loads it from the CDN, and the second one fails. If it's not loaded and the first one can't load, then the second one passes and tries to load it. – Hydrothermal May 03 '17 at 16:32