Js 7
Js 7
In this video we will discuss, where should the script tag be placed in the html.
Should it be placed in the body or head section of the page. In general the script
tag can be placed either in the head or body section.
In Example 1 we placed the script tag in the head section and in Example 2, we
placed it in body section. In both the cases JavaScript works as expected.
<html>
<head>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</form>
<script type="text/javascript">
document.getElementById("TextBox1").style.backgroundColor = "red";
</script>
</body>
</html>
Example 4 : This is same as Example 3, except we moved the script tag to the head
section. In this case the script will not work as expected. Depending on the
browser you are using you get one of the following JavaScript error.
To see these JavaScript errors press F12 which launches developer tools. F12 works
for all the 3 browsers.
<html>
<head>
<script type="text/javascript">
document.getElementById("TextBox1").style.backgroundColor = "red";
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</form>
</body>
</html>
To answer this question, first let's understand what happens when a browser starts
loading a web page.
1. The browser starts parsing the HTML
2. When the parser encounters a <script> tag that references an external JavaScript
file. The parser stops parsing the HTML and the browser makes a request to download
the script file. Until the download is complete, the parser is blocked from parsing
the rest of the HTML on the page.
3. When the download is complete, that's when the parser will resume to parse the
rest of the HTML.
This means the page loading is stopped until all the scripts are loaded which
affects user experience.
In general, the suggested good practice is to place the <script> tag just before
the closing <body> tag, so it doesn't block the HTML parser. However, modern
browsers support async and defer attributes on scripts. These attributes tell the
browser it's safe to continue parsing while the scripts are being downloaded. But
even with these attributes, from a performance standpoint it is still better to
place <script> tag just before closing <body> tag.
JavaScript tutorial
Email This
BlogThis!
Share to Twitter
Share to Facebook
Share to Pinterest
5 comments:
Reply
Reply
Reply
Reply
Reply
Complete Tutorials
How to become a full stack web developer
Cloud computing complete tutorial
JavaScript tutorial
Bootstrap tutorial
Important Videos
The Gift of Education
C tutorial
Angular 6 Tutorial
Angular 2 Tutorial
Design Patterns
SOLID Principles
Bootstrap
AngularJS Tutorial
jQuery Tutorial
JavaScript Tutorial
Charts Tutorial
LINQ
LINQ to SQL
LINQ to XML
Entity Framework
WCF
C#
SQL Server
ADO.NET
ASP.NET
GridView
ASP.NET MVC
Slides
Entity Framework
WCF
C#
SQL Server
ADO.NET
ASP.NET
GridView
ASP.NET MVC
Interview Questions
C#
SQL Server
Written Test
Powered by Blogger.