Properties in JavaScript

In JS there are many terms in use. One of those is property. All objects have properties. One property of a dog is its color. In JS, we might say that mydog.color="black".

Information - The Location Property

Many objects in browser windows have properties. One of the properties of a browser is its location, meaning its URL. We refer to a window's location by using the "window.location=" notation. The script below calls a function that loads a new page determined by the window's location property.

<script language="JavaScript">

function gotoNewPage() {
window.location="http://www.yahoo.com";
}


</script>


Exercise

If we combine the function above with a form button that has an "onclick" we can make the URL change based on students' clicking a button. The complete script is below.

1. Open Notepad and type the script exactly as you see it below.
2. Save it to the desktop.
3. Double click to open it in a browser window.
4. If the script doesn't have typos, the browser window should look something like this.

<html>
<head>
<title>The Location Property of a Window</title>

<script language="JavaScript">
function gotoNewPage() {
window.location="http://www.yahoo.com";
}
</script>
</head>
<h3>Click the button to go to Yahoo</h3>
<form method="post">
<input type="button" value="Click here" onclick="gotoNewPage()">
</form>
</body>
</html>