location

Location

1
Location 对象实际上是 JavaScript 对象,而不是 HTML DOM 对象,下面我们了解一下他的一些属性方法:
1
location <br/>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//注:要在服务器下打开
//设置或返回完整的 URL
var url = location.href;
document.write('url - ' + url + '<br />');
//location.href = 'https://www.baidu.com/'
//设置或返回从井号 (#) 开始的 URL(锚)
var hash = location.hash;
document.write("hash - "+ hash + '<br />');
//设置或返回当前 URL 的主机名
var hostName = location.hostname;
document.write("hostName - "+ hostName + '<br />');
//设置或返回当前 URL 的路径部分
var pathname = location.pathname;
document.write("pathname - "+ pathname + '<br />');
//设置或返回当前 URL 的端口号
var port = location.port;
document.write("port - "+ port + '<br />');
//设置或返回当前 URL 的协议
var porocol = location.porocol;
document.write("porocol - "+ porocol + '<br />');
//设置或返回从问号 (?) 开始的 URL(查询部分)
var search = location.search;
document.write("search - "+ search + '<br />');