To do this, you can use the
async attributes of the
defer will load in a non-blocking manner and only run the code once the page has finished loading, typically in the order they appear on the page.
async and dynamic loading with JS will both run the code as soon as it's downloaded, so execution order is not guaranteed, and it will not wait for the page to finish loading.
Some additional complications include:
- In IE,
defer will not necessarily execute in the order they appear on the page.
async is an HTML5 attribute that has limited browser support.
- All 3 techniques will block the
window.onload event, which extends the page load time.
One work around for the latter is to use a timer with a delay of 0. This won't guarantee that the tag is executed before or after
window.onload, just that it doesn't block the load event.
For the Google+ tag, you don't need to worry about execute order, so you can use either
defer or dynamic loading, depending on whether or not you care about blocking
window.onload. If you have UI-related JS that has to be triggered on
onload, I would probably use the timer technique to minimize the delay.