- This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
Viewing 4 posts - 1 through 4 (of 4 total)
- The forum ‘JavaScript’ is closed to new topics and replies.
The forums ran from 2008-2020 and are now closed and viewable here as an archive.
Home › Forums › JavaScript › Async problem
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” type=”text/javascript”></script>
<script type=”text/javascript”>
document.addEventListener(‘DOMContentLoaded’, function(event) {
cookieChoices.showCookieConsentBar(‘blablablabla’,
‘Close’, ‘Information’, ‘http://www.sito.com’);
});
</script>
example of the bar at the top of the page:
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.
If I load the code after 5 seconds could improve?
That would sort of the defeat the point of using async loading if you ask me. Usually when working with this, one would use readyState
to 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.