In this lesson are presented the JavaScript level one browser objects: navigator and history.

1. navigator object

The navigator object gives some informations about what browser is running your code.
You can use it to collect data about what browsers visit your sites. For example, if you know that Internet Explorer doesn’t support some code, you can test:
      if ( window.navigator.appName == "Microsoft Internet Explorer" ).

navigator object properties

- The following example writes in the page the return values of these properties (used on Firefox).
<script type="text/javascript"><!--
document.write('appCodeName - '+ navigator.appCodeName+ '<br />');
document.write('appName - '+ navigator.appName+ '<br />');
document.write('appVersion - '+ navigator.appVersion+ '<br />');
document.write('cookieEnabled - '+ navigator.cookieEnabled+ '<br />');
document.write('platform - '+ navigator.platform+ '<br />');
document.write('userAgent - '+ navigator.userAgent+ '<br />');
- The result is:
appCodeName - Mozilla
appName - Netscape
appVersion - 5.0 (Windows; en-US)
cookieEnabled - true
platform - Win32
userAgent - Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv: Gecko/20101203 Firefox/3.6.13

navigator object methods

- The following example writes a message about if Java is enabled or not.
<script type="text/javascript"><!--
if (navigator.javaEnabled) document.write('Java enabled');
else document.write('Java is not enabled');

2. history object

The history object contains a list of URLs that the user visited within the current browser window and some features to navigate that list. It is part of the window object and is accessed through the window.history

history object properties

- Example:
<script type="text/javascript"><!--
document.write(window.history.length);      // 3

history object methods

- The fallowing example creates a button to go back two pages:
<script type="text/javascript"><!--
function goBack() {
<button onclick="goBack()">Go Back 2</button>

Daily Test with Code Example

Which attribute is used in <a> tag for the address of the link?
src href rel
<a href="" title=""></a>
Which CSS property sets the type of the text font?
font-family text-decoration font-size
h2 {
What instruction selects all the <div> tags with class="cls"?
querySelector("div.cls") getElementsByTagName("div") querySelectorAll("div.cls")
var elm_list = document.querySelectorAll("div.cls");
var nr_elms = elm_list.length;       // number of selected items
Indicate the function that can be used to get the sum of values in an array.
array_sum() array_diff() array_shift()
$arr = array(1, 2, 3, 4);
$arr_sum = array_sum($arr);
echo $arr_sum;       // 10
navigator and history objects

Last accessed pages

  1. Flash Games - Free online Games (11166)
  2. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (52265)
  3. Get the value of the selected /checked checkboxes in a form (23932)
  4. Zuma Deluxe (27238)
  5. Qwop (20723)

Popular pages this month

  1. Qwop (4729)
  2. Bubbles3 (4343)
  3. Butterfly Kyodai (3092)
  4. Zuma Deluxe (2869)
  5. Backgammon (1464)