How to Use the Navigator Object in JavaScript

There comes a time in every JavaScript programmer's life when it's preferable to work with a single part of a Web page or browser, rather than the entire collection itself. Figuring out which part is what and how to ensure that a script interacts with that part and that part alone isn't always an easy task. However, once you figure out how to do it, it becomes as easy as scripting a simple alert box.

Things You'll Need

  • JavaScript editor
  • Web browser
Show More

Instructions

  1. Use the Navigator Object in JavaScript

    • 1

      Understand that Navigator Objects in JavaScript are the things that every web browser loads into itself, such as HTML documents. They're the frames windows, URLs, browser histories and document forms of a website. They're important to the JavaScript programmer because they allow her to interact with bits and pieces of a web browser as if they were separate items instead of as an entire entity.

    • 2

      Decide which Navigator Object to work with. Some of the most common include the location object, which stores a browser's current URL, the document object, which stores a browser's current web page, the history object, which stores a browser's history list and the window object, which stores a browser's window. Other minor objects include the anchor (link) object and form objects.

    • 3

      Determine which property or method of the Navigator Object to work with. Each object has a list of properties or methods to manipulate. You can access a quick list of each by "dumping" an object's properties onto a web page with a little JavaScript code.

    • 4

      Use this code code to dump (or list) all the properties of a loaded document:
      function DumpProperties(obj, obj_name) {
      result = ""
      For (i in obj)
      result += obj_name + "." + i + " = ' + obj[i] + "\n"
      return result
      }document.writeln(DumProperties(document, documentName))

    • 5

      Replace "obj" with the object that you want to work with and "objname" with the name of the object to see the above code in action. The following example states that the document's title is of interest to a particular JavaScript programmer. For example, document.title.

    • 6

      Determine what you want to do with the object's properties. The following example will change a web page's title to read "I'm waaaaay up here!" Example:
      document.title = "I'm waaaaay up here!"

    • 7

      Use the sample code in your own web page. All you need to do is copy and paste it, then replace the sample values with information particular to your web page.

Tips & Warnings

  • Make sure you test your JavaScripts in at least three browsers, as not all browsers interpret JavaScript in exactly the same way.

Related Searches:

Comments

You May Also Like

Related Ads

Featured