0

I created a vanillaJS script that is used by several other websites. The script-server went down for a couple of minutes last week and the result was that several of the sites using the script were not loading correctly (slow, pages not loading at all, errors, etc.) because the sites kept on "waiting" for the external script to load.

I noticed ALL these sites had the script in de head. I suggested moving the script-tag to the footer plus adding the 'async' attribute. But is this the best solution?

Sidenotes:

  • Can't use jQuery
  • Can't use a framework like Angular or React
  • Preferably don't use additional JS on the site itself
  • The script created it's own content and does not rely on anything on the page it's served on. It simply created a div with content from a datababase staticly served in very basic JS to avoid cross-site errors.

Thanks in advance.

asked May 4, 2017 at 11:46

1 Answer 1

2

async will make the script to load asynchronously and will be be executed while the page is reading. defer will make the script to execute once the page is loaded although it's highly depends on the browser at to my tests IE9, IE8 supports this. You can make a quick check with this defer in fiddler

But there is one more alternative to have the script tag at the bottom as

<script>
window.onload = function() {
 var element = document.createElement("script");
 element.src = "vanillaJS .js";
 document.body.appendChild(element);
};
</script>

But i guess these will not solve your problem where the script is not accessible cause of server fault. I suggest to have the local copy of the script in the website folder and reference it.

answered May 4, 2017 at 12:17
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for the reply. A local copy would be best, but the script is generated dynamically so not an option. Defer at the bottom might be better than async now you've mentioned it.
So is it like you will change the script very often and wanted to see the changes in the clients ?
@mat I have updated the answer, 'defer' will not support most of the browsers.
Thanks Vinod! Yes, the script changes very often. It's sort of a breaking news script. So when anything big happens, it pops up on all sites using the script.The script itself (including content) is created in .Net to avoid JSONP errors.
Then you should try getting the un-cached version of the script when the page is loaded/refreshed. i had the same situation with CRM and i end up doing an Ajax with "If-Modified-Since" & "Cache-Control" request headers.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.