BOM
间歇调用和超时调用(this指向window)
-
超时调用
setTimeout(); 只执行一次,返回一个数值id
参数:1.匿名函数内放要执行的代码;2.时间,单位毫秒
使用clearTimeout(id);通过id值取消指定的超时调用 -
间歇调用
setInterval(); 重复执行,返回一个数值id
参数:1.匿名函数内放要执行的代码;2.时间,单位毫秒
使用clearInterval(id);通过id值取消指定的超时调用 -
网页动态时间
window.onload = function(){
var span = document.getElementsByTagName('span')[0];
var dateStr = formatDate();
//将时间放置到span内
span.innerText = dateStr;
//每隔1秒再放置一次最新的时间
setInterval(function(){
span.innerText = formatDate();
}, 1000);
//格式化年月日时分秒
function formatDate(){
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth()+1;
month = month<10?'0'+month:month;
var day = date.getDate();
day = day<10?'0'+day:day;
var hours = date.getHours();
hours = hours<10?'0'+hours:hours;
var minutes = date.getMinutes();
minutes = minutes<10?'0'+minutes:minutes;
var seconds = date.getSeconds();
seconds = seconds<10?'0'+seconds:seconds;
return year+'-'+month+'-'+day+' '+hours+':'+minutes+':'+seconds;
}
}
效果如下:
系统对话框
- alert() 警告框、弹出框
- confirm() 确认对话框,返回布尔值,点击确定返回true,取消返回false
- prompt() 会话框,点击确认返回输入的值,点击取消返回null
location 当前浏览器的地址栏信息
该对象即是document的属性也是window的属性
console.log(window.location == document.location);//true
属性:
- host 返回服务器名称和端口号
'127.0.0.1:8080'
- hostname返回不带端口号的服务器名称
'127.0.0.1'
- href 返回当前加载页面的完整URL
"http://127.0.0.1:8080/4-location.html"
- pathname 返回URL的目录和文件名
"/4-location.html"
- port 返回URL中指定的端口号
"8080"
- protocol 返回页面使用的协议
"http:"
- search 返回URL的查询字符串,这个字符串以?开头
方法: - assign() 传递一个url参数,打开新url,并在浏览记录中生成一条记录。
location.assign('https://www.baidu.com');
- replace() 传递一个url参数,打开新url,但是不会在浏览记录中生成新记录。
- reload()
重新加载当前页面,参数为布尔值,默认false表示从缓存中重新加载,true会强制清空缓存刷新页面,即从服务器中重新加载。
- 跳转到指定URL,以下四种方式:
location='https://www.baidu.com';
location.href='https://www.baidu.com';
loaction.assign('https://www.baidu.com');
window.open('https://www.baidu.com');
window可以省略
history 该对象保存着用户上网的历史记录
但是只能操作浏览器向前向后翻页
- length 返回历史列表中的网址数
注意:IE9和Opera从0开始,而IE10-11、Firefox、Chrome和Safari从1开始。 - back() 加载 history 列表中的前一个URL
- forward() 加载 history 列表中的下一个URL
- go() 加载 history列表中的某个具体页面
负数表示向后跳转,正数表示向前跳转history.go(1);//向前跳转