Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why insert a static JS dynamically?

Tags:

javascript

On the "Google+ Sign-In for server-side apps" help page, in "Step 3: Include the Google+ script on your page" the following snippet is suggested:

    <!-- The top of file index.html -->
    <html itemscope itemtype="http://schema.org/Article">
    <head>
      <!-- BEGIN Pre-requisites -->
      <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js">
      </script>
      <script type="text/javascript">
        (function () {
          var po = document.createElement('script');
          po.type = 'text/javascript';
          po.async = true;
          po.src = 'https://plus.google.com/js/client:plusone.js?onload=start';
          var s = document.getElementsByTagName('script')[0];
          s.parentNode.insertBefore(po, s);
        })();
      </script>
      <!-- END Pre-requisites -->
    </head>
    <!-- ... -->

Now, what the second SCRIPT seems to do:

  • create a new SCRIPT tag, with static source
  • insert it immediately, before the first SCRIPT tag in the file.

Now, my question is WHAT FOR? I mean, wouldn't doing:

<!-- The top of file index.html -->
<html itemscope itemtype="http://schema.org/Article">
<head>
  <!-- BEGIN Pre-requisites -->
  <script async src="https://plus.google.com/js/client:plusone.js?onload=start"></script>
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
  <!-- END Pre-requisites -->
</head>
<!-- ... -->

achieve the same thing? Why this extra wrapper function inserting the script?

like image 838
SF. Avatar asked Aug 01 '13 14:08

SF.


1 Answers

The async attribute is a fairly new development that isn't supported by most versions of IE. google's code is essentially a cross-browser way to emulate async.

like image 190
giaour Avatar answered Nov 20 '22 03:11

giaour