location对象包含浏览器 地址栏 的信息
href:超文本链接
URL 编码(百分比编码)
URL 编码将字符转换为可通过因特网传输的格式。
URL 只能使用 ASCII 字符集
https://www.w3school.com.cn/charsets/ref_html_ascii.asp 通过因特网进行发送。
由于 URL 通常包含 ASCII 集之外的字符,因此必须将 URL 转换为有效的 ASCII 格式。
URL 编码使用后跟十六进制数字的 "%" 替代不安全的 ASCII 字符。
URL 不能包含空格。URL 编码通常使用加号(+)或 %20 替代空格。
常用属性
href() | 设置或返回完整的URL |
设置href属性,浏览器就会跳转到对应的路径
示例1:location设置
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script>
var str=location.href;
alert(str);
</script>
</head>
<body>
</body>
</html>
示例2:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script>
// location.href="demo.html";
location.href="https://hao.360.com/";
</script>
</head>
<body>
</body>
</html>
示例3:定时跳转页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script>
// location.href="demo.html";
//需求2秒钟之后跳转到www.itheima.com
function run1(){
location.href="https://www.itheima.com/";
}
//此时定时之后直接跳转到另外一个页面,所以两个定时器都可以
// setInterval("run1()",2000);
setTimeout("run1()",2000);
</script>
</head>
<body>
</body>
</html>