Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Load asynchronously external file so in javascript

  • This topic is empty.
Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #252332
    analis
    Participant

    I have a problem I have an external script that slows me down the page loads, it is a lot of code so I can not add it directly in the page, I tried to add the ASYNC attribute, but the script does not work, the alternative is to upload it to delay of a few second but I do not know how to do.

    Can someone help me thank you very much, following the code:

    http://codepen.io/analis/pen/zZBZLN

    #252334
    Shikkediel
    Participant

    Edited – give this a go perhaps…

    codepen.io/anon/pen/bqeWwd

    #252336
    Shikkediel
    Participant

    This version might be better when the script has been cached:

    (function() {
    
      var script = document.getElementsByTagName('script')[0];
    
      if ('cookieChoices' in window) showMessage();
      else script.addEventListener('load', showMessage);
    
    function showMessage() {
    
      cookieChoices.showCookieConsentDialog('Your message for visitors here.', 'close message', 'learn more', 'http://example.com');
    }
    })();
    

    Loading async’s rather tricky…

    #252349
    analis
    Participant

    It works, now my score in google PageSpeed Insights increased by 10 points.

    thank you so much

    #252396
    analis
    Participant

    I did controls, there is a problem function well with chrome, mozilla and safari, but it does not work for Internet Explorer and Microsoft’s edge.

    #252398
    Shikkediel
    Participant

    Please define “does not work” and provide a functional demo because it’s showing like this:

    codepen.io/anon/pen/oZLadJ

    Although I suppose it might not be relevant anymore as PageSpeed isn’t objecting to the script when it’s at the bottom of the page.

    #252401
    analis
    Participant

    Before you edit the bar appeared on all browsers, now with microsoft internet explorer 11 EDGE and the bar does not appear.

    The initial script was at the bottom of the page and with your edit me now 10 points higher. Something must have changed in Google Page Speed because last year I do not signaled as an error.

    #252402
    Shikkediel
    Participant

    When I checked, Google did not subtract points for it…

    Is there any error in the console? Again, without a demo there’s no debugging.

    #252403
    analis
    Participant

    MY SCRIPT (78 points)

    google analytics
    script
    </body>

    or

    script
    google analytics
    </body>

    YOUR SCRIPT

    (83 points)

    google analytics
    script
    </body>

    (89 points, don’ t work in IE and EDGE)

    script
    google analytics
    </body>

Viewing 9 posts - 1 through 9 (of 9 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.