---
title: "JS 关闭当前页面"
author: "可怜的猫"
date: "2016-02-18T09:36:36Z"
tags: []
categories: ["Web前端"]
slug: "js"
draft: false
lastmod: "2016-02-18T09:44:24Z"
id: 2277096
url: "https://lafecat.iteye.com/blog/2277096"
---

相信不少人在开发中都遇到过关闭当前页面这个需求，但面对这么多的浏览器，要做到js的兼容还需要做特殊的处理。关于这方面网上有很多的资料，但大多都是复制粘贴的，没有达到兼容的效果，或者是效果不好。

   下面是我总结的js兼容多浏览器关闭当前页面方法，希望能帮到大家

 

 

var userAgent = navigator.userAgent;

if (userAgent.indexOf("Firefox") != -1 || userAgent.indexOf("Presto") != -1) {

    //window.location.replace("about:blank");

    window.close();

} else {

    window.opener = null;

    window.open("", "\_self");

    window.close();

}