// 1.location对象 // (1) 获取url地址 var urlStr = location.href; console.log('urlStr:', urlStr); // (2) 用location.href进行页面跳转 location.href = 'http://www.baidu.com'; // (2) 页面参数传递,比如a页面要传数据给b页面 location.href = 'http://www.baidu.com?a=222&b=333'; // (3) location.search 获取浏览器地址?开始的参数 // 作业: 把字符 ?a=123&b=456,使用对象存起来,比如: {a:123,b:456} // var str = '?a=123&b=456'; // var obj = {}; var params = location.search;
// (4) 延迟执行 setTimeout(function() { console.log('一秒后执行') }, 1000); // (5) setInterval 定时器 var count = 0; var timer = setInterval(function() { console.log(count++); // 当count等于10的时候,清除定时器 if (count === 10) { // 清除定时器 clearInterval(timer); } }, 1000);
// 导航 // navigator // history对象 history.back(); history.forward(); history.go();
location.href 获取浏览器地址 (window.location)location.href = 'http://www.baidu.com' // 跳转到百度location.href = 'http://www.baidu.com?username=huruqing'// 跳转到百度的时候,加上参数 username=huruqingsetTimeout 延迟setInterval 定时器和清除定时器navigator: userAgent 用来判断用户在使用什么设备
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title></head><body> <h3>正在判断用户设备......</h3> <script> // 判断用户使用设备 //js判断是否移动端 function fIsMobile() { return /Android|iPhone|iPad|iPod|BlackBerry|webOS|Windows Phone|SymbianOS|IEMobile|Opera Mini/i.test(navigator.userAgent); } setTimeout(function() { var res = fIsMobile(); if (res) { location.href = './mobile.html'; } else { location.href = './pc.html'; } }, 3000); </script></body></html>
history对象 浏览器历史对象history.back();history.forward();history.go();