JavaScript Location对象

9 篇文章 0 订阅

location Location - Window


Summary

Returns a Location object, which contains information about the URL of the document and provides methods for changing that URL. You can also assign to this property to load another URL.

Syntax

var locationObj = window.location;
 window.location = newLocation;
 

where

  • locationObj is an object of type Location, providing information about the current URL and methods to change it. Its properties and methods are described below.
  • newLocation is a Location object or a string, specifying the URL to navigate to.

Location object

Location objects have a toString method returning the current URL. You can also assign a string to document.location. This means that you can work with document.location as if it were a string in most cases. Sometimes, for example when you need to call a String method on it, you have to explicitly call toString:

alert(document.location.toString().charAt(17))
 

Properties

All of the following properties are strings. You can read them to get information about the current URL or set them to navigate to another URL.

The "Example" column contains the values of the properties of the following URL:

  • http://www.google.com:80/search?q=devmo#test
PropertyDescriptionExample
hashthe part of the URL that follows the # symbol.#test
hostthe host name and port number.www.google.com:80
hostnamethe host name (without the port number).www.google.com
hrefthe entire URL.http://www.google.com:80/search?q=devmo#test
pathnamethe path (relative to the host)./search
portthe port number of the URL.80
protocolthe protocol of the URL.http:
searchthe part of the URL that follows the?? symbol, including the?? symbol.?q=devmo

If the hash part of the URL contains encoded characters (see Core_JavaScript_1.5_Reference:Global_Functions:encodeURIComponent), hash returns the decoded URL part. This is a bug in Firefox. href, search and pathname return the correct, encoded URL parts. For example:

  • http://www.google.com/search?q=Fire%20fox#Fire%20fox

results in:

  • hash=#Fire fox
  • search=?q=Fire%20fox

Methods

MethodDescription
assign(url)Load the document at the provided URL.
reload(forceget)Reload the document from the current URL. forceget is a boolean, which, when it is true, causes the page to always be reloaded from the server. If it is false or not specified, the browser may reload the page from its cache.
replace(url)Replace the current document with the one at the provided URL. The difference from the assign() method is that after using replace() the current page will not be saved in session history, meaning the user won't be able to use the Back button to navigate to it.
toString()Returns the string representation of the Location object's URL. See the JavaScript reference for details.

Example

Whenever a property of the location object is modified, a document will be loaded using the URL as if window.location.assign() had been called with the modified URL.

Replace the current document with the one at the given URL:

 function goMoz() {
 window.location = "http://www.mozilla.org";
 }
 
 // in html: <button οnclick="goMoz();">Mozilla</button>
 
 

Display the properties of the current URL in an alert dialog:

 function showLoc()
 {
 var x = window.location;
 var t = ['Property - Typeof - Value',
 'window.location - ' + (typeof x) + ' - ' + x ];
 for (var prop in x){
 t.push(prop + ' - ' + (typeof x[prop]) + ' - ' +  (x[prop] || 'n/a'));
 }
 alert(t.join('\n'));
 }
 
 // in html: <button οnclick="showLoc();">Show location properties</button>
 
 

Send a string of data to the server by modifying the search property:

 function sendData(dat)
 {
 window.location.search = dat;
 }
 
 // in html: <button οnclick="sendData('Some data');">Send data</button>
 
 

The current URL with "?Some%20data" appended is sent to the server (if no action is taken by the server, the current document is reloaded with the modified search string).

Specification

DOM Level 0. Not part of any standard.

@compat

IE6|IE7|FF1|FF2|FF3|OPERA|SAFARI2|SAFARI3|KONQ
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值