Where should jQuery code go in header or footer?


It’s always a good practice to add jQuery code in footer i.e. just before the closing </body> tag. If you have not done that, then use the defer attribute.

Use defer attribute so the web browser knows to download your scripts after the HTML downloaded −

<script src="new.js" defer="defer"></script>

The defer attribute is used to specify that the script execution occurs when the page loads. It is useful only for external scripts and is a boolean attribute.

Example

The following code shows how to use the defer attribute −

<!DOCTYPE html>
<html>
   <body>
      <script src="new.js" defer></script>
      <p>The external file added will load later, since we're using defer</p>
   </body>
</html>


Ali
Ali

Updated on: 20-Feb-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements