window.location

Tutorial: Using JavaScript's Location object to work with URLs

<script type="text/javascript"><!-- google_ad_client = "pub-4361182986188898"; /* ArtViewTopLgeRecV3OpBelTit */ google_ad_slot = "9906576005"; google_ad_width = 336; google_ad_height = 280; //--> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script> <script>window.google_render_ad();</script>

Level: Intermediate. Published on 28 July 2008 in JavaScript

Learn how to use the JavaScript Location object to read and change URLs, as well as how to reload (or refresh) the page.

JavaScript gives you many ways to access and change the current URL that is displayed in the visitor's browser. All these techniques use the Location object, which is itself a property of the Window object. You can create a new Location object that contains the current URL as follows:


var currentURL = window.location;

In this tutorial, you explore all the properties and methods of the Location object. You learn:

  • How to read all the different parts of a URL
  • How to send the visitor to another page by changing the URL, and
  • How to automatically reload or refresh the page.

The anatomy of a URL

A URL can be broken down into as many as six components, though many of them are optional:


<protocol> //<hostname> :<port> /<pathname> <search> <hash>

The only required components of a URL are the protocol and the hostname; all other components are optional.

Here's an example of a URL with all these components:


http://www.example.com:80/example.cgi?x=3&y=4#results

In this example, http: is the protocol, www.example.com is the hostname, 80 is the port, /example.cgi is the pathname, ?x=3&y=4 is the search, or query string, and #results is the hash, or the anchor to display within the page.

Reading the current URL through the Location object

You can access all of these URL components using Location properties of the same names. You can also access the following properties:

host
Contains the hostname and the port combined into one string, e.g. www.example.com:80
href
Contains the complete URL, e.g. http://www.example.com:80/example.cgi?x=3&y=4#results

Here are some examples (assume that the example URL above is displayed in the browser's address bar at the time the code below is run):


var currentURL = window.location;
alert ( currentURL.href ); // Displays 'http://www.example.com:80/example.cgi?x=3&y=4#results'
alert ( currentURL.protocol ); // Displays 'http:'
alert ( currentURL.host ); // Displays 'www.example.com:80'
alert ( currentURL.hostname ); // Displays 'www.example.com'
alert ( currentURL.port ); // Displays '80'
alert ( currentURL.pathname ); // Displays '/example.cgi'
alert ( currentURL.search ); // Displays '?x=3&y=4'
alert ( currentURL.hash ); // Displays '#results'

Manipulating the URL using the Location object

You can use the href property of the Location object to send visitors to a completely different page:


window.location.href = "http://www.example.com/anotherpage.html";

Here's a simple example:


<input type="button" onclick="window.location.href='http://www.google.com/'" value="Visit www.google.com" />

Here's the resulting button. Click it to visit www.google.com if you like, then use your browser's Back button to return here.

Setting the Location.href property like this keeps the previous URL in the browser's history, so the visitor can click the Back button to return to the previous page. If you don't want the visitor to be able to go back to the previous URL, use Location.replace() instead:


window.location.replace ( "http://www.example.com/anotherpage.html" );

This loads the new URL, and also replaces the current history entry with the new URL, so that the previous URL is effectively wiped from the history.

As well as redirecting the browser to a different URL, you can selectively change parts of the current URL using the Location object properties. For example, to change the URL's hash to a different anchor within the page:


window.location.hash = "#moreResults";

Here's an example:


<input type="button" onclick="window.location.hash='#top'" value="Jump to the top of the page" />

...and here's the resulting button. Click it, and your browser moves to the top of the page. That's because this page contains an anchor named #top at the top. Notice how the URL changes in the browser's address bar. (You can click your browser's Back button afterwards to return here.)

Reloading the page

Finally, you can use the Location.reload() method to force the browser to reload the current URL, just as if the visitor had clicked their Reload/Refresh button:


window.location.reload ( );

To force the browser to retrieve the page directly from the server, bypassing any caches, pass a true parameter to Location.reload() :


window.location.reload ( true );

Here's an example that uses Location.reload() to create a button to refresh the page:


<input type="button" onclick="window.location.reload()" value="Reload the page" />

Here's the button — click it to see it in action!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值