BOM

123 阅读2分钟

1.js中定义的全局变量和全局方法都会放在window对象下 eg window.alert("hello") //alert() window.prompt("please input a number")

【参考代码】

hello world

打开页面
回到顶部
click

2.var OBtn =document.getElementById("btn"); 通过ID获得元素 scrollTo(x,y) 页面滚动到指定的位置 OBtn.onclick=function(){ window.scrollTo(0,0); } scrollBy(x,y)页面滚动了多少距离 OBtn1.onclick=function(){ window.scrollBy(100,100); } 在新窗口打开指定链接页面 OBtn2.onclick=function(){ var url="网址" window.open(url) } 确认对话框 包含确定和取消按钮 会返回一个Boolean值 var tag=confirm("are you sure?") console.log(tag)

【参考代码】

3 定时器 var i=60; var timer = setInterval( function(){ consloe.log(i+"秒之后可以再次发送") i-- if(i<=50){ clearInterval(timer);【清除定时器】 } },1000【1000毫秒 1秒】 ) 【timer没有特别的含义 它是一个number类型的数 可以理解为页面中的第几个定时器】 【每隔n秒执行的方法 有两个参数 第一个参数是方法 function 第二个参数是时间间隔 (以毫秒为单位)】

 异步和同步
  setTimeout(function(){ 【异步方法】
  console.log(1)
},0);
console.log(2);
console.log(3);

【输出结果为 2 3 1】 【js是单线程的】 【 代码顺序执行遇到异步方法(setTimeout)会把这个方法放进任务队列里】 【当主线程的任务都执行完毕后 会将任务队列中的方法取出 并且看等待时间间隔是否达到,如果时间到了就执行这个方法】 4.history【对象 浏览器的访问历史记录】 back() 返回上一页面 go(-1) forward() 进入下一页面 go(1) go(number) 返回或进入number页面 【参考代码】

页面1

点击进入页面二 进入下一个页面 前进两步

5.Navigator 格式 var ua=navigator.userAgent; if(ua.indexOf("iphone")!=-1){ setTimeout(function(){ location.href="www.baidu.com" },1000) }else{ setTimeout(function(){ location.href="www.didichuxing.com" },1000) }

6.location scheme:http https https 相对安全 http 相对不安全 file 计算机上的文件 https 访问的页面 资源 http://xxx 报错 http 访问的页面 资源 https://xxx 可行 scheme 因特网访问的类型 host 定义域主机 (http 的默认主机 www) location.replace 替换当前页面 history中没有历史记录 location.herf 跳转页面 history中有历史记录 location.reload 重新加载