Dom中的常用对象location,该对象类似于java中的URL对象。下面的html中有部分属性的示例代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>DOM的常用对象(一)</title>
</head>
<body>
<script type="text/javascript">
/*
<----location对象----->
1.此对象包含当前的URL信息,属性有以下几个:
1> hrft 设置或获取整个URL为一个字串
2> protocol 设置或获取URL对应的协议部分
3> pathname 设置或获取location或者URL对应的主机名称
4> port 设置或获取与URL关联的端口号
5> host 设置或获取location或URL对应的主机名和端口号
6> hostname 设置或获取location或URL对应的主机名
7> search 设置或获取属性href中问号后的内容
2. 对象包含的方法:
1> assign 装入新的html文档
2> reload 重新载入当前页面
3> replace 装入指定URL的文档来替换当前文档
通过location对象的属性进行获取/设置属性
*/
/*属性的获取*/
function locationGetAttributes() {
var pro = location.protocol;
var href = location.href;
var pathName = location.pathname;
alert("pro-->" + pro + "--href-->" + href + "--pathName-->" + pathName);
}
/*属性的设置*/
function locationSetAttributes() {
//设置后会自动跳转到指定的链接
location.href = "http://www.baidu.com";
}
/*方法演示*/
function locationAssign() {
location.assign("http://www.tmall.com/?spm=1.6659421.754904973.1.fxF2zs");
}
function locationReload() {
location.reload();
}
function locationReplace() {
location.replace("http://www.baidu.com");
}
</script>
<!--事件源-->
<input type="button" name="getbutton" value="获取属性" οnclick="locationGetAttributes()" />
<input type="button" name="setbutton" value="设置属性" οnclick="locationSetAttributes()" />
<hr />
<div>
<input type="button" name="assignbutton" value="assign" οnclick="locationAssign()" />
<input type="button" name="reloadbuton" value="reload" οnclick="locationReload()" />
<input type="button" name="replacebutton" value="replace" οnclick="locationReplace()" />
</div>
</body>
</html>