Difference between revisions of "JavaScript Location Object"

From Techotopia
Jump to: navigation, search
(JavaScript Location Object Methods)
(JavaScript Location Object Methods)
Line 41: Line 41:
 
* '''location.reload()''' - reloads the current document. The same as pressing the ''Reload'' button in the browser window. If ''true'' is passed through as an argument (i.e ''location.reload(true)'') it forces a reload without using the browser cache (similar to shift clicking on the browser ''Reload'' button.
 
* '''location.reload()''' - reloads the current document. The same as pressing the ''Reload'' button in the browser window. If ''true'' is passed through as an argument (i.e ''location.reload(true)'') it forces a reload without using the browser cache (similar to shift clicking on the browser ''Reload'' button.
  
* '''location.replace()''' - replaces the current URL with the new URL specified as an argument (i.e ''location.replace(<nowiki>"http://www.techotopia.com"</nowiki>)''. Change does not get recording the browser's history.
+
* '''location.replace()''' - replaces the current URL with the new URL specified as an argument (i.e ''location.replace(<nowiki>"http://www.techotopia.com"</nowiki>)''. Change does not get recorded to the browser's history (see [[JavaScript History Object]]).

Revision as of 17:07, 18 May 2007

The JavaScript Location object is a child object of the Window Object and is used to store information about the current URL of the Window object.


Contents


Loading a New URL into the Current Window

The location object contains a number of methods and properties that enable the JavaScript developer to manipluate the current URL of a window. A commom example is to progammatically change the current window URL. This can be done by assigning a new URL string to the href property of the Location option:

window.location.href="http://www.techotopia.com";

Reading the URL of the Current Window

It is also possible to read the URL of the current document. The simplest form of this is to access the href property of the Location object of the current window:

var currentURL=window.location.href;

Extracting Parts of the URL

It is also possible to access the various components that go to make up a complete URL. Suppose, for example, that the current URL is:

http://www.techotopia.com:80/order.cgi?batch=1#intro

the following properties represent the various elements of the URL:

  • location.protocol - the protocol section of the URL (for example http: or https:)
  • location.hostname - the hostname (for example www.techotopia.com)
  • location.port - the HTTP Port number of the URL (for example 80)
  • location.search - the search portion of the URL (batch=1 in the example above)
  • location.hash - the anchor name in the URL (#intro in our example)

JavaScript Location Object Methods

The JavaScript Location Object contains two methods that may be invoked from JavaScript. These are:

  • location.reload() - reloads the current document. The same as pressing the Reload button in the browser window. If true is passed through as an argument (i.e location.reload(true)) it forces a reload without using the browser cache (similar to shift clicking on the browser Reload button.
  • location.replace() - replaces the current URL with the new URL specified as an argument (i.e location.replace("http://www.techotopia.com"). Change does not get recorded to the browser's history (see JavaScript History Object).