Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where in the HTML file should Jquery and Bootstrap be placed?

Curious at to where I place my Jquery and Bootstrap files. They recommend that you always place at the bottom for performance purposes yet when I check sites that use Jquery/Bootstrap the majority of users always place them at the top. Also should I be loading my own JavaScript files before or after the bootstrap/Jquery files?

I take it that I load the my own css file first before the bootstrap file if I want to override some of their styling, is this correct and does the same apply to javascript files?

like image 937
Mlbliner Avatar asked May 27 '15 16:05

Mlbliner


People also ask

Where should I put jQuery in Bootstrap?

Place the following <script> s near the end of your pages, right before the closing </body> tag, to enable them. jQuery must come first, then Popper. js, and then our JavaScript plugins.

Where do I put jQuery link in HTML?

We can link jQuery in an HTML page by using a script tag and providing the downloaded jQuery library file address as the src attribute. The jquery-3.6. 0.

What is the best location for including the jQuery library in your HTML file?

It is actually best practice to load jQuery at the very end of the body element. This way all of your images load before jQuery.

Why should we include the jQuery file while using Bootstrap?

JQuery and Bootstrap CDN – Why You Should Use JQuery CDN on Your Website. JQuery CDN allows you to include jQuery on your website without the need to download the program in your website folder. When you use jQuery for your Bootstrap themed website, you decrease the load on your website.


2 Answers

Typically stylesheets in the head and scripts before the closing </body> tag:

<html>   <head>     <link rel="stylesheet" href="bootstrap.css">     <link rel="stylesheet" href="your-other-styles.css">   </head>   <body>     <!-- content -->     <script src="jquery.js"></script>     <script src="bootstrap.js"></script>     <script src="your-other-scripts.js"></script>   </body> </html> 

You'll want files from vendors such as jQuery and Bootstrap to be included before yours. This means that:

  • CSS: You can override their styles with your own*
  • Scripts: You have access to any objects added to the global scope such as window (jQuery adds $, for example)

However, if you require a script to be available before your content loads (such as Modernizr), then putting it in the <head> ensures it's ready before any of your content.

Including scripts at the bottom ensures that the actual page content is loaded first; when the scripts are finally downloaded the content (DOM) will be ready for your scripts to manipulate.

* assuming your selector specificity is at least equal to those in your vendor CSS

like image 90
Alex Avatar answered Oct 11 '22 18:10

Alex


Bottom is best to place all your script references at the end of the page before </body>.It should look like below in normal page.

<html>  <head>    <link href="path/to/file.css" rel="stylesheet" type="text/css">  </head>  <body>    <script src="path/to/file.js" type="text/javascript"></script>  </body> </html> 

Although in some cases it may be necessary to load JavaScript before page load if any of your function need to access JavaScript before Page Load.Specially if you are working with JQuery UI And Bootstrap.

You can decorate your script tags with the defer attribute so that the browser knows to download your scripts after the HTML has been downloaded:

<script src="Jquery.js" type="text/javascript" defer="defer"></script> 

or

  <script src="demo_async.js" async></script>  

When present, it specifies that the script will be executed asynchronously as soon as it is available.

http://www.w3schools.com/tags/att_script_async.asp

If you need script to access in page for use then script need to available before using it. Although it need to be sure the browser support defer="defer". Async is supported by all major browsers.

Javascript by default block any other parallel downloads. So if you have many tags in the head, calling on multiple external scripts will block the HTML from loading, thus greeting the user with a blank white screen, because no other content on your page will load until the java script files have completely loaded. Another advantage to load script in bottom is that any error caused by external script will not stop the page from Loading to browser.

Style css need to present in top <Head> to access in page.

like image 30
Garry Avatar answered Oct 11 '22 20:10

Garry