Forums

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

Home Forums JavaScript Async problem

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

    Hi I have a problem I can not solve.

    I’m trying to load this code asynchronously to speed up page loading. I plug in the external file ASYNC. The page loads faster but the bar is not displayed, removing the ASYNC bar appears but the slow page loads.

    Can you help me? thank you

    <script async src=”http://www.segnalidivita.com/cookiechoices.js&#8221; type=”text/javascript”></script>

    <script type=”text/javascript”>
    document.addEventListener(‘DOMContentLoaded’, function(event) {
    cookieChoices.showCookieConsentBar(‘blablablabla’,
    ‘Close’, ‘Information’, ‘http://www.sito.com&#8217;);
    });
    </script>

    example of the bar at the top of the page:

    http://www.segnalidivita.com/

    #237972
    Shikkediel
    Participant

    Using async doesn’t mix very well with DOMContentLoaded. I think easiest would be to listen for the window load event instead. Although in this case, I don’t think there’s a speed advantage since the script is already at the bottom of the page.

    #237984
    analis
    Participant

    If I load the code after 5 seconds could improve?

    #238001
    Shikkediel
    Participant

    That would sort of the defeat the point of using async loading if you ask me. Usually when working with this, one would use readyStateto determine if the document is ready to be manipulated.

    Window loaded would be a better approach than setting a timeout. Or listening to the onload event of the script itself and execute it whenever that happens…

    I recently made this work on my own site and it’s quite complicated.

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