Helping ordinary people create extraordinary websites!

JavaScript Redirect

(and current URL

Often you will want to redirect the user to another web page, either on your website or another. This can be easily achieved in JavaScript with a redirect via the document.location property.

document.location = "http://www.yahoo.com/";

Identify current URL

As document.location is simply a variable, you can retrieve the current URL from it:

var currenturl = document.location

For the exact location, document.location.href can also be used. In addition to href, document.location also has other properties covering the parts of the URL, including "protocol", "host", "hostname", "port", "pathname", "search" and "hash". For example, for http://developertutorials.com:80/, the values are the following:

(blank, no :port in url, http defaults to :80)
href "http://developertutorials.com/scripts/7/?someparam=somevalue#someid"
protocol "http:"
host "developertutorials.com"
hostname "developertutorials.com"
port
pathname "/scripts/7/"
search ?someparam=somevalue
hash #someid

When working with these Redirects, however, it is important not to rely entirely on them.

« JavaScript Popup Windows JavaScript Cookies »


More JavaScript Tutorials:
» How to Disable the Submit Button
» Validating Form Input - II
» Pre-Fill Forms From Last Use
» Making a JavaScript Tree Menuing System
» Top 10 Custom JavaScript Functions Of All Time
» 9 Javascripts You Better Not Miss!


GET OUR NEWSLETTERS