What is the best place to put <script> tag in a JSP file?

11,538

Solution 1

In the browser JSP will be converted to/interpreted as HTML, so the best practices are the same for both JSP and HTML, saying that:

Many web developers recommend loading JavaScript code at the bottom of the page to increase responsiveness, and this is even more important with the HTML service. In the NATIVE sandbox mode, all scripts you load are scanned and sanitized client-side, which may take a couple of seconds.

Moving your tags to the end of your page will let HTML content render before the JavaScript is processed, allowing you to present a spinner or other message to the user.

And for further reading take a look at :

Solution 2

The old approach to solve this problem was to put tags at the bottom of your , because this ensures the parser isn't blocked until the very end.

This approach has its own problem: the browser cannot start downloading the scripts until the entire document is parsed. For larger websites with large scripts & stylesheets, being able to download the script as soon as possible is very important for performance. If your website doesn't load within 2 seconds, people will go to another website.

In an optimal solution, the browser would start downloading your scripts as soon as possible, while at the same time parsing the rest of your document.

The modern approach

Today, browsers support the async and defer attributes on scripts. These attributes tell the browser it's safe to continue parsing while the scripts are being downloaded.

async

<script type="text/javascript" src="path/to/script1.js" async></script>
<script type="text/javascript" src="path/to/script2.js" async></script>

Scripts with the async attribute are executed asynchronously. This means the script is executed as soon as it's downloaded, without blocking the browser in the meantime. This implies that it's possible script 2 is downloaded & executed before script 1.

According to http://caniuse.com/#search=async, 80% of all browsers support this.

defer

<script type="text/javascript" src="path/to/script1.js" defer></script>
<script type="text/javascript" src="path/to/script2.js" defer></script>

Scripts with the defer attribute are executed in order (i.e. first script 1, then script 2). This also does not block the browser.

Unlike async scripts, defer scripts are only executed after the entire document has been loaded.

According to http://caniuse.com/#search=defer, 80% of all browsers support this. 88% support it at least partially.

An important note on browser compatibility: in some circumstances IE <= 9 may execute deferred scripts out of order. If you need to support those browsers, please read this first!

Conclusion

The current state-of-the-art is to put scripts in the tag and use the async or defer attributes. This allows your scripts to be downloaded asap without blocking your browser.

The good thing is that your website should still load correctly on the 20% of browsers that do not support these attributes, while speeding up the other 80%.

Source

Share:
11,538
Crusaderpyro
Author by

Crusaderpyro

01010011 01101100 01100101 01100101 01110000 00100000 01100100 01101001 01100100 00100000 01101110 01101111 01110100 00100000 01101000 01101111 01101110 01101111 01110010 00100000 01101101 01100101 00100000 01110111 01101001 01110100 01101000 00100000 01101001 01110100 00011001 01110011 00100000 01110000 01110010 01100101 01110011 01100101 01101110 01100011 01100101 00101110 00100000 01010100 01101000 01100001 01110100 00100111 01110011 00100000 01100001 01101100 01101100 00100000 01001001 00100000 01101000 01100001 01110110 01100101 00100000 01110100 01101111 00100000 01110011 01100001 01111001 00100000 01100001 01100010 01101111 01110101 01110100 00100000 01101101 01111001 01110011 01100101 01101100 01100110 00101110

Updated on June 05, 2022

Comments

  • Crusaderpyro
    Crusaderpyro almost 2 years

    I have a bunch of JSP files and around 10-12 javascipt files which I am including in every JSP file using the include tag as follows:

       <%@ include file="common_js_files.jsp"%>
    

    I have already seen Where should I put <script> tags in HTML markup?

    As per the answers given, many agreed to place the <script> tags just before the closing body tag. I was expecting this would reduce the page loading time, however it didn't change the loading time. Is the case mentioned in the above link only for HTML pages ? What about JSP pages ?

    Kindly suggest if there is a better way of handling such common javascript files in JSP pages.

    Thanks in advance.