Helping ordinary people create extraordinary websites!
GET OUR NEWSLETTER
Your Email:
 

JavaScript Redirect

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 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, however, it is important not to rely entirely on them as they

« JavaScript Popup Windows JavaScript Cookies »

More JavaScript Tutorials:
» Hide and Display Content On Demand
» Striped Tables Using JavaScript
» Looping through form elements using JavaScript
» Things To Do With Textarea Form Fields
» Introduction to JavaScript Tutorial
» Validating Form Input I
Advertise with Us!


Learn JavaScript Tutorials Scripts