• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

10. Page Navigation Techniques > 10.1. Loading a New Page or Anchor

Loading a New Page or Anchor

NN 2, IE 3

Problem

You want a script (instead of a hardwired link) to perform the navigation to another page or anchor.

Solution

To load a different page into the current window or frame, assign the new page’s URL string to the location.href property:

location.href = "http://www.megacorp.com/products/framistan309.html";

To navigate to an anchor on the current page, assign the anchor’s name string (the value assigned to the a element’s name attribute) to the location.hash property:

location.hash = "section03";

Discussion

The URL value you assign to the location.href property can be a relative or complete URL, in string form. A relative URL is influenced by any <basehref> tag that may be delivered with the page (sometimes a server is configured to deliver this tag with all pages, and it is occasionally visible in a browser’s source view). Because an assignment statement using location.href unloads the current page, you cannot count on any script statements below this one to execute before the page and all its variables and values disappear (although some browsers seem to operate a little bit ahead).


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint