Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a script to load last on webpage?

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>
...
like image 927
wahle509 Avatar asked Dec 12 '12 18:12

wahle509


People also ask

Do script tags load in order?

Script tags are executed in the order they appearFunctionally this means you can significantly slow down your site if you have slow scripts loading early in the page. It also means scripts which appear later on the page can depend on things scripts which appear earlier have done.

How do you add a script to the end in HTML?

Adding JavaScript into an HTML Document You can add JavaScript code in an HTML document by employing the dedicated HTML tag <script> that wraps around JavaScript code. The <script> tag can be placed in the <head> section of your HTML or in the <body> section, depending on when you want the JavaScript to load.

How do you load third party code after your Page has primarily finished loading?

Use async or defer # The async and defer attributes tell the browser that it may go on parsing the HTML while loading the script in the background, and then execute the script after it loads. This way, script downloads don't block DOM construction and page rendering.


1 Answers

Add the defer attribute to the script tag in the head also works

<script language="JavaScript" src="http://..." type="text/javascript" defer></script>
like image 91
Amit Creation Avatar answered Oct 02 '22 18:10

Amit Creation