<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>navigator对象及属性(userAgent)(扩展)</title> <!-- Navigator 对象包含有关浏览器的信息 navigator.userAgent;用户使用的系统信息及浏览器信息 --> </head> <body> <script> var browse,nav=navigator.userAgent; console.log(nav); function na(){ var browse,explorer=navigator.userAgent.toLowerCase();//获取浏览器信息并将其变成小写 if (explorer.indexOf("trident")>-1){ //indexOf如果找到ie会返回ie的索引值,如果找不到会返回-1, browse="IE"; }else if(explorer.indexOf("chrome")>-1){ browse="chrome"; }else if(explorer.indexOf("opera")>-1){ browse="opera"; }else if(explorer.indexOf("safari")>-1){ browse="safari"; } return browse; } var bro=na(); alert("当前为:"+bro+"浏览器"); </script> </body> </html>