JavaScript ASP Tutorial 11th lesson--application Object _asp Foundation

Source: Internet
Author: User
Tags session id

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, we saw in Lesson 09. I ' ll demonstrate it again down below.

By contrast, the Contents collection is demonstrated above. It allows to set and retrieve variables with application scope. The format for setting a application variable is application.contents ("VariableName") = "VariableValue" . However, since Contents is the default Collection, we have a little shortcut for setting application. 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, and I are on the same ASP Web site at the same time. If You are set the background color to white in script11.asp, then I 'll have a "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 This 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 does not support Model Apartment Vior.

Application Methods:

The two application Methods are Lock () and Unlock (). Lock () blocks all other users from changing the application variables. Unlock () releases control so and other users can change it. That's important on a larger site where multiple users of May is trying to access the same page in 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 this 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. ' ll also notice a call to the ADO Library. We'll talk about that while we get to databases.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.