How to get a script to load last on webpage?

html, javascript, load, rss

Solution

Two options:

- Put your script block at the end of the page. This is good practice anyways due to the fact that your page strucutre will load before the script that is intended to manipulate it. Because the user generally notices the page loading but not the script, this gives the appearance of a faster load overall.

- Include the `defer` attribute in the opening `script` tag. Note that `defer` can only be used on external script files (those with an `src` attribute). See https://developer.mozilla.org/en-US/docs/HTML/Element/script.

Problem

I have a webpage with mostly basic HTML on it. There is one section where I load an RSS feed using JavaScript pulling from an external source (url). The problem is that my page will load everything up until that script, wait for the script to load (sometimes up to a few seconds), then load the rest of the page. How can I force it to load the script after it has rendered the entire page first? My code is something like this: ``` <html> <more html> <script language="JavaScript" src="http://..." type="text/javascript"></script> <more html> ... ```

Original source

Related problems