# 打开、关闭当前网页

# 脚本打开网页

  1. 在新标签页打开

window.open('url', '_blank')

  1. 在当前页面打开

window.open('url', '_self')

# 关闭浏览器键入url方式打开的网页

window.location.href = "about:blank"
window.close()
1
2

# 关闭脚本打开的网页

window.opener = null
window.open("about:blank", "_self", "")
window.close()
1
2
3

上代码:

export const isPc = () => {
  const ua = navigator.userAgent
  const moveDevices = [
    "Android",
    "iPhone",
    "SymbianOS",
    "Windows Phone",
    "iPad",
    "iPod"
  ]
  let flag = true;
  for (let i = 0; i < moveDevices.length; i++) {
    if (ua.indexOf(moveDevices[i]) !== -1) {
      flag = false;
      break
    }
  }
  return flag
}

export const closeCurWebPage = () => {
  if (!isPc()) {
    window.opener = null
    window.open("about:blank", "_self", "").close()
  }
  if (navigator.userAgent.indexOf(Firefox) !== -1 || navigator.userAgent.indexOf("Chrome") !== -1) {
    // 浏览器键入url方式打开的网页
    window.location.href = "about:blank"
    window.close()
  } else {
    // 脚本打开的网页
    window.opener = null
    window.open("about:blank", "_self", "")
    window.close()
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36