1.window

window是浏览器的一个实例,在浏览器中,window对象有双重角色,它既是通过Javascript访问浏览器窗口的一个接口,又是ECMAScript规定的Global对象(全局对象)。

  1. var age =15;
  2. //声明一个全局变量
  3. window.name="chengchao";
  4. //相当于var name = "chengchao"

2.Window对象的方法

window.alert()
window.confirm(“msg”)

  1. <div>
  2. <span id="mi">小米5</span>
  3. <button id="btn">删除</button>
  4. </div>
  1. var mi = document.getElementById("mi");
  2. var btn = document.getElementById("btn");
  3. btn.onclick = function () {
  4. var result = window.confirm("你确定删除吗");
  5. if (result) {
  6. mi.parentNode.removeChild(mi);
  7. }
  8. }

QQ截图20201124161710.jpg
window.prompt(“text,defaultText”)

  1. var test = window.prompt("请输入","hello world");
  2. console.log(test)

QQ截图20201124162351.jpg

3.定时器

超时调用-setTimeout()

  1. setTimeout(function(){
  2. console.log("hello world")
  3. },2000)

间歇调用-setInterval()

  1. setInterval(function(){
  2. console.log("1")
  3. },1000)

4.清除定时器

clearInterval()

  1. <button id="btn">停止定时器</button>
  1. /* 设置定时器的时候,会在window下挂载一个属性 */
  2. var btn = document.getElementById("btn");
  3. var temp = setInterval(function(){
  4. console.log("2")
  5. },1000)
  6. btn.onclick = function(){
  7. clearInterval(temp);
  8. }

5.递归

函数调用函数自身,就叫递归

  1. function show(){
  2. setTimeout(function(){
  3. console.log(1)
  4. show();
  5. },1000)
  6. }
  7. show()

6.location

  1. location.href是一个跳转函数
  2. <button id="btn">跳转到百度</button>
  1. var btn = document.getElementById("btn");
  2. btn.onclick = function(){
  3. console.log(location.port)
  4. }

7.history

能够实现返回上一步

02.html

  1. <p>02</p>
  2. <a href="02history.back.html">03</a>

0.2history.html

  1. <p>03</p>
  2. <button id="btn"> btn</button>
  1. var btn =document.getElementById("btn");
  2. btn.onclick=function(){
  3. history.back();
  4. }

8.screen与navigator方法

screen

  1. // 获取客户端关于屏幕的信息
  2. var screenWidth = window.screen.availWidth;
  3. //获取可视区域的width
  4. var viewWidth = document.body.clientWidth;
  5. console.log(screenWidth)
  6. console.log(viewWidth)

navigator

  1. console.log(navigator.userAgent)

pc
image.png

ipad
image.png

mobile
image.png

  1. //检测浏览器类型
  2. if(/Android|iphone|webOS/i.test(navigator.userAgent)){
  3. location.href="mobile.html"
  4. }else if(/ipad/i.test(navigator.userAgent)){
  5. location.href="pad.html"
  6. }