Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
692 views
in Technique[技术] by (71.8m points)

what's the difference between <% %> and <script language="vbscript" runat="server"> in classic asp?

I couldn't find much documentation on the web

so far now, the obvious difference seems to be that you cant mix html and vbscript using the "script" tag

for example, this is ok

<% public sub display_literal() %>
  literal
&lt% end sub %>

but with the script tag you should


<script language="vbscript" runat="server">
public sub display_literal2()
    response.write "literal2</br>"
end sub
</script>

on this page

http://www.newobjects.com/pages/ndl/alp/asp-structure.htm

it says that

In classic ASP the script written in the default script language for the page (i.e. the language assumed for the <% %> tags) is executed second - e.g. all the script code in <% %> tags is initialized after all the <SCRIPT RUNAT=SERVER ...> scripts.

but I made a couple of tests and couldn't verify it...

I'm asking because I had a script (I don't have it at hand right now) that using <% %> gave me an error, changing it to the <script> tag solved the problem, but I'd like to know why....

anyway, I guess that we should use the <script> tag for functions and procedures that are to be called from <% %> tags... right?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First off you need to understand that there's a difference in the way server-side script tags are handled depending on whether the language specified is the same as the default language for the page.

The order is this:-

  1. Run all scripts in <script runat="server" tags where the language specified does not match the default language. These are executed in document order.
  2. Run the default script. That means perform the implied writes to the response where there is static content in the page (the stuff not in runat="server" tags or inside <% %>) and any intervening code in <% %> again in document order obviously.
  3. Run any code at the global level found in <script runat="server" tags where the language matches the default script language.

Note that all script has an initial parse before executing phase 1, hence any functions that may be defined by scripts run in phase 3 will be available for calling from phase 1.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...