I have just started to work on web applications, and learning to use ASP.Net. I have come across two methods to add script in an HTML page. One is by using script tag, and other one is by using <% ... %>
tag. But, I am unable to figure out, what is the difference between both, and which one should I prefer in which case?
The script tag is used to specify full scripts (usually complete methods/function) (mostly client side scripts but you can also have server side script blocks) while the <% ... %> are used to include server side inline snippets. Save this answer. Show activity on this post.
The <script> tag is used to embed a client-side script (JavaScript). The <script> element either contains scripting statements, or it points to an external script file through the src attribute. Common uses for JavaScript are image manipulation, form validation, and dynamic changes of content.
async: It is used to specify the script is executed asynchronously. charset: It is used to specify the character encoding used in an external script file. defer: It is used to specify that the script is executed when the page has finished parsing. src: It is used to specify the URL of an external script file.
JavaScript in head: A JavaScript function is placed inside the head section of an HTML page and the function is invoked when a button is clicked. JavaScript in body: A JavaScript function is placed inside the body section of an HTML page and the function is invoked when a button is clicked.
The script tag is used to specify full scripts (usually complete methods/function) (mostly client side scripts but you can also have server side script blocks) while the <% ... %>
are used to include server side inline snippets.
The tag can be JavaScript or the language in question. The <% %> is functionally equivalent to
<script language="C#|vb" runat="server">
In general, you put the code in code behind (win forms), so the only is for client side script (generally JavaScript). With ASP.NET MVC, the <% %> is for marking up the view and is not really code.
Script tags are used to add Javascript (or similar) script to the final HTML which is rendered by the browser, and hence allow client-side scripting.
<% ... %>
tags are ASP tags that are processed on the server side, and will not be present in the rendered HTML.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With