HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
The Global.asa file is an optional file that can contain declarations of objects, variables, and methods thatcan be accessed by every page in an ASP application
The Global.asa file is an optional file that can contain declarations of objects, variables, and methods that can be accessed by every page in an ASP application.
All valid browser scripts (JavaScript, VBScript, JScript, PerlScript, etc.) can be used within Global.asa.
The Global.asa file can contain only the following:
In Global.asa you can tell the application and session objects what to do when the application/session starts and what to do when the application/session ends. The code for this is placed in event handlers. The Global.asa file can contain four types of events:
Application_OnStart
A Global.asa file could look something like this:
It is possible to create objects with session or application scope in Global.asa by using the <object> tag.
Syntax
Parameter | Description |
---|---|
scope | Sets the scope of the object (either Session or Application) |
id | Specifies a unique id for the object |
ProgID | An id associated with a class id. The format for ProgID is [Vendor.]Component[.Version]
Either ProgID or ClassID must be specified. |
ClassID | Specifies a unique id for a COM class object. Either ProgID or ClassID must be specified. |
Examples
The first example creates an object of session scope named "MyAd" by using the ProgID parameter:
The second example creates an object of application scope named "MyConnection" by using the ClassID parameter:
The objects declared in the Global.asa file can be used by any script in the application:
A TypeLibrary is a container for the contents of a DLL file corresponding to a COM object. By including a call to the TypeLibrary in the Global.asa file, the constants of the COM object can be accessed, and errors can be better reported by the ASP code. If your Web application relies on COM objects that have declared data types in type libraries, you can declare the type libraries in Global.asa.
Syntax
Parameter | Description |
---|---|
file | Specifies an absolute path to a type library.
Either the file parameter or the uuid parameter is required |
uuid | Specifies a unique identifier for the type library.
Either the file parameter or the uuid parameter is required |
version | Optional. Used for selecting version. If the requested version is not found, then the most recent version is used |
lcid | Optional. The locale identifier to be used for the type library |
Error Values
The server can return one of the following error messages:
Error Code | Description |
---|---|
ASP 0222 | Invalid type library specification |
ASP 0223 | Type library not found |
ASP 0224 | Type library cannot be loaded |
ASP 0225 | Type library cannot be wrapped |
Restrictions on what you can include in the Global.asa file:
Global.asa is often used to initialize variables.
The example below shows how to detect the exact time a visitor first arrives on a Web site. The time is stored in a Session variable named "started", and the value of the "started" variable can be accessed from any ASP page in the application:
Global.asa can also be used to control page access.
The example below shows how to redirect every new visitor to another page, in this case to a page called "newpage":
And you can include functions in the Global.asa file.
In the example below the Application_OnStart subroutine occurs when the Web server starts. Then the Application_OnStart subroutine calls another subroutine named "getcustomers". The "getcustomers" subroutine opens a database and retrieves a record set from the "customers" table. The record set is assigned to an array, where it can be accessed from any ASP page without querying the database:
In this example we will create a Global.asa file that counts the number of current visitors.
The Global.asa file:
To display the number of current visitors in an ASP file: