常用javascript方法
字体相关
设置网页根字体
1 | document.getElementsByTagName("html")[0].setAttribute("style", "font-size:" + window.innerWidth / 10 + "px") |
获取页面上实际字体大小
1 | window.getComputedStyle(document.querySelector('html'),null).getPropertyValue('font-size') |
使页面上的字体大小不随系统设置的字体改变
1 | document.getElementsByTagName("html")[0].setAttribute("style", "font-size:" + 16*16/window.getComputedStyle(document.querySelector('html'),null).getPropertyValue('font-size').replace('px','') + "px") |
浏览器版本相关
获取浏览器ua
1 | var ua = window.navigator.userAgent; |
判断浏览器版本
1 | function getBrowser() { |
判断是否为ios
1 | function isIOS() { |
判断是否为企业微信
1 | function isWechat() { |
url相关
获取base URL
1 | const getBaseURL = url => url.replace(/[?#].*$/, '') |
判断URL是否绝对
1 | const isAbsoluteURL = str => /^[a-z][a-z0-9+.-]*:/.test(str) |
获取url参数作为对象
1 | const getURLParameters = url => |
硬件相关
获取选中文字
1 | const getSelectedText = () => window.getSelection().toString() |
复制文字到剪贴板
1 | const copyToClipboard = str => { |
切换全屏模式
1 | const fullscreen = (mode = true, el = 'body') => |
查看用户的首选语言
1 | const detectLanguage = (defaultLang = 'en-US') => |
查看设备是否支持触屏操作
1 | const supportsTouchEvents = () => |
移动端判断横竖屏
1 | window.addEventListener('orientationchange', function(event){ |
其他
生成UUID
1 | const UUIDGeneratorBrowser = () => |
禁止鼠标右键和F12
1 | <script> |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 NaClO's Blog!
评论
