Java Reference
In-Depth Information
These two pages are incredibly simple. Starting with the legacy.html page, you check if navigator
.geolocation is a truthy value:
if (navigator.geolocation) {
location.replace("modern.html");
}
If it is, you redirect the user to the modern.html page. Note that you use replace() rather than
href , because you don't want the user to be able to click the browser's Back button. This way it's
less easy to spot that a new page is being loaded.
The modern.html page is almost identical, except that in your if statement you check if navigator
.geolocation is falsey:
if (!navigator.geolocation) {
location.replace("legacy.html");
}
If so, you redirect to legacy.html .
exercise 2 Question
Modify Example 3 to display one of the four images randomly. Hint: refer to Chapter 5 and the
Math.random() method.
exercise 2 Solution
<!DOCTYPE html>
<html lang="en">
<head>
<title>Chapter 8, Question 2</title>
</head>
<body>
<img src="" width="200" height="150" alt="My Image" />
<script>
function getRandomNumber(min, max) {
return Math.floor(Math.random() * max) + min;
}
var myImages = [
"usa.gif",
"canada.gif",
"jamaica.gif",
"mexico.gif"
];
var random = getRandomNumber(0, myImages.length);
document.images[0].src = myImages[random];
</script>
</body>
</html>
Search WWH ::




Custom Search