zoukankan      html  css  js  c++  java
  • 实用JS代码

    浏览器功能

    1.0 浏览器判断

    如下代码判断是手机还是电脑访问的网站

     1 function IsPC () {
     2 
     3   var userAgentInfo = navigator.userAgent
     4 
     5   var Agents = ['Android', 'iPhone', 'SymbianOS', 'Windows Phone', 'iPad', 'iPod']
     6 
     7   var flag = true
     8 
     9   for (var i = 0; i < Agents.length; i++) {
    10 
    11     if (userAgentInfo.indexOf(Agents[i]) > 0) {
    12 
    13       flag = false
    14 
    15       break
    16 
    17     }
    18 
    19   }
    20 
    21   return flag
    22 
    23 }

    判断IE,微信,安卓

     1 // ie浏览器
     2 
     3 function isIE () {
     4 
     5   var userAgent = navigator.userAgent
     6 
     7   return userAgent.indexOf('compatible') > -1 && userAgent.indexOf('MSIE') > -1 && !isOpera
     8 
     9 }
    10 
    11 // 微信浏览器
    12 
    13 function isWeiChat () {
    14 
    15   var userAgent = navigator.userAgent
    16 
    17   return userAgent.toLowerCase().indexOf('micromessenger') > -1 // 微信浏览器
    18 
    19 }
    20 
    21 // 是否是安卓
    22 
    23 function isAndroid () {
    24 
    25   var userAgent = navigator.userAgent
    26 
    27   return userAgent.indexOf('Android') > -1 || userAgent.indexOf('Adr') > -1
    28 
    29 }

    具体信息

     1 function BrowserType () {
     2 
     3   var userAgent = navigator.userAgent
     4 
     5   var isOpera = userAgent.indexOf('Opera') > -1
     6 
     7   var isIE = userAgent.indexOf('compatible') > -1 && userAgent.indexOf('MSIE') > -1 && !isOpera
     8 
     9   var isEdge = userAgent.indexOf('Edge') > -1
    10 
    11   var isFF = userAgent.indexOf('Firefox') > -1
    12 
    13   var isSafari = userAgent.indexOf('Safari') > -1 && userAgent.indexOf('Chrome') == -1
    14 
    15   var isChrome = userAgent.indexOf('Chrome') > -1 && userAgent.indexOf('Safari') > -1
    16 
    17   if (isIE) {
    18 
    19     var reIE = new RegExp('MSIE (\d+\.\d+);').test(userAgent)
    20 
    21     var fIEVersion = parseFloat(RegExp['$1'])
    22 
    23     
    24 
    25     if (fIEVersion === 7) {
    26 
    27       return 'IE7'
    28 
    29     } else if (fIEVersion === 8) {
    30 
    31       return 'IE8'
    32 
    33     } else if (fIEVersion === 9) {
    34 
    35       return 'IE9'
    36 
    37     } else if (fIEVersion === 10) {
    38 
    39       return 'IE10'
    40 
    41     } else if (fIEVersion === 11) {
    42 
    43       return 'IE11'
    44 
    45     } else {
    46 
    47       return null //IE版本过低
    48 
    49   }
    50 
    51   if (isFF) {
    52 
    53     return 'FF'
    54 
    55   } else if (isOpera) {
    56 
    57     return 'Opera'
    58 
    59   } else if (isSafari) {
    60 
    61     return 'Safari'
    62 
    63   } else if (isChrome) {
    64 
    65     return 'Chrome'
    66 
    67   } else if (isEdge) {
    68 
    69     return 'Edge'
    70 
    71   }
    72 
    73 }

    1.1 复制黏贴

    完成复制功能

     1 // 复制
     2 
     3 function copy () {
     4 
     5  var input = document.getElementById('input')
     6 
     7  input.select()
     8 
     9  document.execCommand('Copy')
    10 
    11 }
    12 
    13 document.execCommand('SelectAll') // 打开
    14 
    15 document.execCommand('SaveAs') // 另存为
    16 
    17 document.execCommand('Print') // 打印
    18 
    19 document.execCommand('Cut', 'false', null) // 剪切
    1.2 页面宽高
    用如下代码
     1 document.body.clientWidth // 网页可见区域宽
     2 
     3 document.body.clientHeight // 网页可见区域高
     4 
     5 document.body.offsetWidth // 网页可见区域宽 (包括边线的宽)
     6 
     7 document.body.offsetHeight // 网页可见区域高 (包括边线的高)
     8 
     9 document.body.scrollWidth // 网页正文全文宽
    10 
    11 document.body.scrollHeight // 网页正文全文高
    12 
    13 document.body.scrollTop // 网页被卷去的高
    14 
    15 document.body.scrollLeft // 网页被卷去的左
    16 
    17 window.screenTop // 网页正文部分上
    18 
    19 window.screenLeft // 网页正文部分左
    20 
    21 window.screen.height // 屏幕分辨率的高
    22 
    23 window.screen.width // 屏幕分辨率的宽
    24 
    25 window.screen.availHeight // 屏幕可用工作区高度
    26 
    27 window.screen.availWidth // 屏幕可用工作区宽度
    1.3 cookie

    js操作cookie

     1 // 设置cookies
     2 
     3 function setCookie (op) {
     4 
     5   var key = op.key
     6 
     7   var value = op.value
     8 
     9   var time = op.time || 2 * 24 * 60 * 60 * 1000 // 默认2天
    10 
    11   var url = op.url ? ';path=/;domain=.' + op.url : ''
    12 
    13   var exp = new Date()
    14 
    15   exp.setTime(exp.getTime() + time)
    16 
    17   document.cookie = key + '=' + escape(value) + ';expires=' + exp.toGMTString() + url
    18 
    19 }
    20 
    21 // 获取cookies
    22 
    23 function getCookie (key) {
    24 
    25   if (typeof key === 'string') {
    26 
    27     return getOne(key)
    28 
    29   } else if (key instanceof Array) {
    30 
    31     var cookie = {}
    32 
    33     key.forEach(function (item) {
    34 
    35       cookie[item] = getOne(item)
    36 
    37     })
    38 
    39     return cookie
    40 
    41   }
    42 
    43   
    44 
    45   function getOne (name) {
    46 
    47     var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)')
    48 
    49     var arr = document.cookie.match(reg)
    50 
    51     if (arr) return unescape(arr[2])
    52 
    53     return null
    54 
    55   }
    56 
    57 }
    58 
    59 // 移除Cookie
    60 
    61 function removeCookie (key) {
    62 
    63   var exp = new Date()
    64 
    65   exp.setTime(exp.getTime() - 1)
    66 
    67   var value = getCookie(key)
    68 
    69   if(value != null) document.cookie = key + "=" + value + ";expires=" + exp.toGMTString()
    70 
    71 }
    1 setTimeout(function(a){console.log(a)}, 2000, 'done');//done
    2 //setTimeout用法,setTimeout(回调函数,时间,参数1,...,参数n)。
  • 相关阅读:
    编写一个函数func(),将此函数的输入参数(int型)逆序输出显示,如54321 –> 12345,要求使用递归,并且函数体代码不超过8行
    java中两种单例模式
    springMVC配置freemarker 二(问题讨论篇)
    springMVC配置freemarker
    java中@value的环境配置
    java环境log4j日志环境的配置。
    websocket协议
    http报文和浏览器缓存机制
    详解网络连接
    编码总结
  • 原文地址:https://www.cnblogs.com/studyshufei/p/8532252.html
Copyright © 2011-2022 走看看