欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

javascript asp教程第十一课--Application 对象

程序员文章站 2023-11-16 21:53:52
overview: the application object represents a collection of asp pages. the applicati...

overview:

the application object represents a collection of asp pages. the application object has zero (0) properties, two (2) collections, two (2) methods, and two (2) events.

get started:

below are a couple scripts for lesson11.

<%@language="javascript"%>
<%
//no asp here, just a regular html page
%>
<html>
<form action="script11a.asp" method="post">
<strong>would you like to chose the official application color?</strong><br>
<select name="officialcolor">
<option>red</option>
<option>white</option>
<option>blue</option>
</select><br>
<input type="submit" value="yes, click here">
</form>
no, <a href="script11a.asp">click here</a>.
</html>

click here to run script11.asp in a new window. below is script11a.asp.

<%@language="javascript"%>
<html>
<%
application.lock()
var officialcolor = new string( request.form("officialcolor") )
if (officialcolor=="red")
	{
	application("officialcolor") = "red"
	}
if (officialcolor=="white")
	{
	application("officialcolor") = "white"
	}
if (officialcolor=="blue")
	{
	application("officialcolor") = "blue"
	}
if (officialcolor=="undefined")
	{
	//do nothing. just leave the color alone.
	}
application.unlock()
%>
<body bgcolor="<%=application("officialcolor")%>">
<strong>
the official application color is <%=application("officialcolor")%>.<br>
</strong>
</body>
</html>

application collections:

the two collections are contents and staticobjects. staticobjects come in the form of the <object> flag. that's done in the global.asa that we saw in lesson 09. i'll demonstrate it again down below.

by contrast, the contents collection is demonstrated above. it allows you to set and retrieve variables with application scope. the format for setting an application variable is application.contents("variablename")="variablevalue". however, since contents is the default collection, we have a little shortcut for setting application variables. the shortcut goes like this: application("variablename")="variablevalue".

share the variable:

one last thought on application variables. the application variable is shared by all visitors. let's say that you and i are on the same asp web site at the same time. if you set the background color to white in script11.asp, then i will see "white" as the official color if i go directly to script11a.asp. application variables are by no means the most efficient means of passing information from one user to another.

no apartment model:

you may notice that the example above seems to be inefficient. why not just directly assign application("officialcolor") = request.form("officialcolor")? because we can't. by design, the newer (and updated) versions of microsoft server software intentionally do not support apartment model behavior.

application methods:

the two application methods are lock() and unlock(). lock() blocks all other users from changing the application variables. unlock() releases control so that other users can change it. that's important on a larger site where multiple users may be trying to access the same page at the same time.

application events:

the two application events are application_onstart() and application_onend(). these events are accessed in the global.asa. let's repeat that script from lesson 09.

<object runat=server scope=session id=myinfo progid="mswc.myinfo">
</object>

<script runat="server" language="javascript">
function application_onstart()
	{
	application("somevariablename")="some value"
	}
function application_onend()
	{
	application.contents.removeall()
	}
function session_onstart()
	{
	session.timeout=15
	}
function session_onend()
	{
	//do nothing
	}
</script>

<!-- metadata type="typelib" 
file="c:\program files\common files\system\ado\msado15.dll" 
-->

misc. items:

application.contents has two methods of its own. application.contents.remove("variablename") and application.contents.removeall().

you'll notice there are also session events in the global.asa. we'll deal with those in lesson 12. you'll also notice a call to the ado library. we'll talk about that when we get to databases.