Archive for June 22, 2007

JavaScript: How to Load a New Browser URL Page

Posted in JavaScript, Software, Web, internet, programming on June 22, 2007 by Joey

There are a few different ways in which a new page can be loaded in a browser through JavaScript.

window.document.location.href = 'http://www.joeyjavas.com';
window.document.location = 'http://www.joeyjavas.com';
window.location = 'http://www.joeyjavas.com';
location = 'http://www.joeyjavas.com';
document.location.href = 'http://www.joeyjavas.com';
document.location = 'http://www.joeyjavas.com';

As seen in the previous examples, both the window object and the document object have a location property that can be assigned a URL. It is generally preferable to set the location property of the window object, as the document object was originally intended as a read-only property and some older browsers still treat it as such.

Relative or absolute URLs can be assigned as the page to be loaded.

There is also a replace method that can be called on the location property, as follows:

window.location.replace('http://www.joeyjavas.com');

Using this method will cause the requested page to replace the current page in the browser and is not a new page request. Hence, the page it replaces is no longer in the browser history.