What is the difference between script tag, and "<%....%>" tag?
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 whic开发者_StackOverflow中文版h 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.
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.
<% %>
is for asp.net coding and <script> tag can use for any language by specifying which language you want to use like this one <script type="text/javascript">
for javascript
What do you mean by script
tag?
<script language="C#" runat="server">
...
</script>
this is used when you specify that you do not want to place the server code in a separated file, such as myFile.aspx.cs
or in VB myFile.aspx.vb
, this will be the code to run before the page is rendered and will act in compliance with the ASP.NET Page Events Cycle.
The tags <% %>
are used to place Server Code in your page, just like plain old Classic ASP.
Normally we use as Response.Write
using <%= ... %>
but if you use Resources Files, you will end up using <%$ ResourceFile.Variable %>
as well
It's a mean to inject server code into the page
the script tag indicates where client script is going to be executed or if in a function where it is to be organized. The <% .. %> is code that is executed on the webserver and is never seen by the client.
<script>
window.alert("hi") // This will popup a hi message
</script>
<% responce.wrtie("hi") %>
When you load this page you would see a popup and the text hi however in the page source you would see
<script>
window.alert("hi") // This will popup a hi message
</script>
hi
Particularly in case of ASP.net
Code inside script tag is directly placed directly under the class that is generated by the asp.net when the aspx page is requested.
(Every time a request is made to .aspx page , the page is parsed into a .cs file whose path can be found out by using <%=GetType().Assembly.Location%>.And also you have to set the Debug=true in the page directive.)
Code inside <% %> is placed inside a method in the generated class file. For example you can not write Response.Write() directly inside the script tag as it needs to be written inside a method.
Hope this helps.
精彩评论