Another object that we
may have occasion to use is the location object. The location object is a property
of the window object...
window.location
And, like other objects, it has properties. One commonly used property is the
href property...
window.location.href
This specifies the URL of the document in that particular window.
Look at this script...
<HTML>
<HEAD>
<TITLE></TITLE>
<SCRIPT language="Javascript"><!--
function ShowUrl()
{
alert(window.location.href);
}
function GoUrl()
{
window.location.href = "otherpage.html";
}
//--></SCRIPT>
</HEAD>
<BODY>
<A HREF="javascript:ShowUrl()">Click here</A> for this
document's URL.<BR>
<A HREF="javascript:GoUrl()">Click here</A> to go to another
page.
</BODY>
</HTML>
Try it.
The function ShowUrl() gets the location and displays it in an alert box. The
function GoUrl() sets the window's location to something else thereby causing
that other page to load in the window.
Exercise: Make 6 separate little web pages for each of the brady kids.
Something like this is fine. Alter the last exercise and instead of an alert
box throwing up the value "bobby", change the value to the url of
bobby's page (bobby.html, or whatever you name it.) Then, get that value and
set that as the window's new href. The result is a nifty jump box.
We can define our own arrays. Let's suppose we want to define an array of colors...
colors
red
blue
green
yellow
purple
orange
First we would declare a new array...
colors = new Array();
Then define the individual elements of the array...
colors = new Array();
colors[0] = "red";
colors[1] = "blue";
colors[2] = "green";
colors[3] = "yellow";
colors[4] = "purple";
colors[5] = "orange";
Again, note the zero-based counting scheme. The colors array above has 6 elements,
and we can reference each by number. Consider the following...
<HTML>
<HEAD>
<TITLE></TITLE>
<SCRIPT language="Javascript"><!--
colors = new Array();
colors[0] = "red";
colors[1] = "blue";
colors[2] = "green";
colors[3] = "yellow";
colors[4] = "purple";
colors[5] = "orange";
function GetMyColor()
{
alert(colors[2]);
}
//--></SCRIPT>
</HEAD>
<BODY>
<A HREF="javascript:GetMyColor()">Click here for my color</A>
</BODY>
</HTML>
Try it.
While it's certainly not the most useful script in the world, it does demonstrate
an array quite nicely. Understand what's going on here before you continue.
Next>> | ||||||||||||||||||||||||||||||||
|
© All rights reserved to Wajahat Ali |