How can I reorganize script tags?

I have script tags in <head>so that they don't ask for any security errors when navigating between SSL and non-SSL pages. But he just looks hairy.

Anyway, can I combine them or reduce a part of the code?

<script type="text/javascript">document.write(["\<script src='",("https:" == document.location.protocol) ? "https://" : "http://","ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js' type='text/javascript'>\<\/script>"].join(''));</script>
<script type="text/javascript">document.write(["\<script src='",("https:" == document.location.protocol) ? "https://" : "http://","html5shiv.googlecode.com/svn/trunk/html5.js' type='text/javascript'>\<\/script>"].join(''));</script>
<script type="text/javascript">document.write(["\<script src='",("https:" == document.location.protocol) ? "https://" : "http://","use.typekit.com/12345.js' type='text/javascript'>\<\/script>"].join(''));</script>
+3
source share
2 answers

All modern browsers will understand the relative link URI format, which will work automatically for both URIhttp and https schema names :

<script src="//example.com/path/script.js"></script>

Further reading:

+5
source
<script type="text/javascript">
  // Create a closure for our loadScript-function
  var loadScript = (function () {
       var headTag = document.getElementsByTagName('head')[0],
           scriptTagTemplate = document.createElement('script'),
           addEventListener = function (type, element, listener) {
              if(element.addEventListener) {
                 element.addEventListener(type, listener);
                 return true;
              }
              else if (element.attachEvent) {
                 element.attachEvent('on'+type, listener);
                 return true;
              }
              return false;
           };
       scriptTagTemplate.type = 'text/javascript';

       // This function clones the script template element and appends it to head
       // It takes an uri on the form www.example.com/path, and an optional
       // callback to invoke when the resource is loaded.
       return function (uri, callback) {
          var scriptTag = scriptTagTemplate.cloneNode(true);
          if (callback) {
              addEventListener('load', scriptTag, callback);
          }
          headTag.appendChild(scriptTag);
          scriptTag.src = ['//', uri].join('');
       }
    }());
</script>

โ€‹โ€‹:

<script>
        loadScript('ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js');
        loadScript('html5shiv.googlecode.com/svn/trunk/html5.js');
        loadScript('use.typekit.com/12345.js');
</script>

, document.write, . script.

0

Source: https://habr.com/ru/post/1744837/


All Articles