原生JavaScript技巧大收集(1~10)
1、原生JavaScript实现字符串长度截取
01 |
function cutstr(str, len) { |
02 |
var temp; |
03 |
var icount = 0; |
04 |
var patrn = /[^\x00-\xff]/; |
05 |
var strre = "" ; |
06 |
for ( var i = 0; i < str.length; i++) { |
07 |
if (icount < len - 1) { |
08 |
temp = str.substr(i, 1); |
09 |
if (patrn.exec(temp) == null ) { |
10 |
icount = icount + 1 |
11 |
} else { |
12 |
icount = icount + 2 |
13 |
} |
14 |
strre += temp |
15 |
} else { |
16 |
break |
17 |
} |
18 |
} |
19 |
return strre + "..." |
20 |
} |
2、原生JavaScript获取域名主机
01 |
function getHost(url) { |
02 |
var host = "null" ; |
03 |
if ( typeof url == "undefined" || null == url) { |
04 |
url = window.location.href; |
05 |
} |
06 |
var regex = /^\w+\:\/\/([^\/]*).*/; |
07 |
var match = url.match(regex); |
08 |
if ( typeof match != "undefined" && null != match) { |
09 |
host = match[1]; |
10 |
} |
11 |
return host; |
12 |
} |
3、原生JavaScript清除空格
1 |
String.prototype.trim = function () { |
2 |
var reExtraSpace = /^\s*(.*?)\s+$/; |
3 |
return this .replace(reExtraSpace, "$1" ) |
4 |
} |
4、原生JavaScript替换全部
1 |
String.prototype.replaceAll = function (s1, s2) { |
2 |
return this .replace( new RegExp(s1, "gm" ), s2) |
3 |
} |
5、原生JavaScript转义html标签
1 |
function HtmlEncode(text) { |
2 |
return text.replace(/&/g, '&' ).replace(/\"/g, '"' ).replace(/</g, '<' ).replace(/>/g, '>' ) |
3 |
} |
6、原生JavaScript还原html标签
1 |
function HtmlDecode(text) { |
2 |
return text.replace(/&/g, '&' ).replace(/"/g, '\"' ).replace(/</g, '<' ).replace(/>/g, '>' ) |
3 |
} |
7、原生JavaScript时间日期格式转换
01 |
Date.prototype.Format = function (formatStr) { |
02 |
var str = formatStr; |
03 |
var Week = [ '日' , '一' , '二' , '三' , '四' , '五' , '六' ]; |
04 |
str = str.replace(/yyyy|YYYY/, this .getFullYear()); |
05 |
str = str.replace(/yy|YY/, ( this .getYear() % 100) > 9 ? ( this .getYear() % 100).toString() : '0' + ( this .getYear() % 100)); |
06 |
str = str.replace(/MM/, ( this .getMonth() + 1) > 9 ? ( this .getMonth() + 1).toString() : '0' + ( this .getMonth() + 1)); |
07 |
str = str.replace(/M/g, ( this .getMonth() + 1)); |
08 |
str = str.replace(/w|W/g, Week[ this .getDay()]); |
09 |
str = str.replace(/dd|DD/, this .getDate() > 9 ? this .getDate().toString() : '0' + this .getDate()); |
10 |
str = str.replace(/d|D/g, this .getDate()); |
11 |
str = str.replace(/hh|HH/, this .getHours() > 9 ? this .getHours().toString() : '0' + this .getHours()); |
12 |
str = str.replace(/h|H/g, this .getHours()); |
13 |
str = str.replace(/mm/, this .getMinutes() > 9 ? this .getMinutes().toString() : '0' + this .getMinutes()); |
14 |
str = str.replace(/m/g, this .getMinutes()); |
15 |
str = str.replace(/ss|SS/, this .getSeconds() > 9 ? this .getSeconds().toString() : '0' + this .getSeconds()); |
16 |
str = str.replace(/s|S/g, this .getSeconds()); |
17 |
return str |
18 |
} |
8、原生JavaScript判断是否为数字类型
1 |
function isDigit(value) { |
2 |
var patrn = /^[0-9]*$/; |
3 |
if (patrn.exec(value) == null || value == "" ) { |
4 |
return false |
5 |
} else { |
6 |
return true |
7 |
} |
8 |
} |
9、原生JavaScript设置cookie值
1 |
function setCookie(name, value, Hours) { |
2 |
var d = new Date(); |
3 |
var offset = 8; |
4 |
var utc = d.getTime() + (d.getTimezoneOffset() * 60000); |
5 |
var nd = utc + (3600000 * offset); |
6 |
var exp = new Date(nd); |
7 |
exp.setTime(exp.getTime() + Hours * 60 * 60 * 1000); |
8 |
document.cookie = name + "=" + escape(value) + ";path=/;expires=" + exp.toGMTString() + ";domain=360doc.com;" |
9 |
} |
10、原生JavaScript获取cookie值
1 |
function getCookie(name) { |
2 |
var arr = document.cookie.match( new RegExp( "(^| )" + name + "=([^;]*)(;|$)" )); |
3 |
if (arr != null ) return unescape(arr[2]); |
4 |
return null |
5 |
} |
原生JavaScript技巧大收集(1~10)的更多相关文章
- 原生JavaScript技巧大收集100个
原生JavaScript技巧大收集 1.原生JavaScript实现字符串长度截取function cutstr(str, len) { var temp; var icount = 0; var p ...
- 原生JavaScript技巧大收集
原生JavaScript技巧大收集 地址:http://itindex.net/detail/47244-javascript
- 原生JavaScript技巧大收集(11~20)-(终于又被我找到这篇文章了)
11.原生JavaScript加入收藏夹 function AddFavorite(sURL, sTitle) { try { window.external.addFavorite(sURL, sT ...
- 原生态纯JavaScript 100大技巧大收集---你值得拥有
1.原生JavaScript实现字符串长度截取 function cutstr(str, len) { var temp; var icount = 0; var patrn = /[^\x00-\x ...
- 10个原生JavaScript技巧
这些代码片段主要由网友们平时分享的作品代码里面和经常去逛网站然后查看源文件收集到的.把平时网站上常用的一些实用功能代码片段通通收集起来,方便网友们学习使用,利用好的话可以加快网友们的开发速度,提高工作 ...
- 分享10个原生JavaScript技巧
首先在这里要非常感谢无私分享作品的网友们,这些代码片段主要由网友们平时分享的作品代码里面和经常去逛网站然后查看源文件收集到的.把平时网站上常用的一些实用功能代码片段通通收集起来,方便网友们学习使用,利 ...
- javascript技巧大全套
事件源对象 event.srcElement.tagName event.srcElement.type 捕获释放 event.srcElement.setCapture(); event.srcE ...
- 原生JavaScript技巧
时常在技术论坛有看见一些比较好的示例,于是就出于一种收集并学习的态度,于是就保留下来啦~ 当然现在展示的也只是一部分,先放一部分出来尝尝鲜~~~
- JavaScript常用,继承,原生JavaScript实现classList
原文链接:http://caibaojian.com/8-javascript-attention.html 基于 Class 的组件最佳实践(Class Based Components) 基于 C ...
随机推荐
- Mac 上flink的安装与启动
在Mac 上安装flink,需要通过Homebrew安装的 1.howmebrew的安装方式,在终端粘贴以下命令,或者去官网https://brew.sh/index_zh-cn 找到此代码复制粘贴到 ...
- ANSYS渡槽槽身动水压力的施加(1)——矩形渡槽
前言 依据水工抗震规范中关于渡槽动水压力的部分编一个用于ANSYS渡槽模型动水压力施加的命令流,是我研究生时一直想要做的一件事,原因嘛主要是想对比一下规范提供的方法和ANSYS声学流体单元模拟水体这两 ...
- ERP条码解决方案,金蝶盘点机条码解决方案,应用PDA的信息化管理能给我们的生产管理带来怎么样的变化的探讨
ERP条码解决方案,金蝶盘点机条码解决方案,应用PDA的信息化管理能给我们的生产管理带来怎么样的变化的探讨. 当前越来越多的大大小小的中国企业已经接受了ERP的思想,大多数的商店,企业,工厂都会上一套 ...
- 区块链--Bitcoin共识机制
目录 中心化和去中心化 比特币共识机制 拜占庭将军共识机制 比特币成功解决了拜占庭问题 中心化和去中心化 中心化模式: 优点:效率高 缺点:中间层次太多(组织层次连接) 去中心化模式: 缺点:效率低 ...
- [T-ARA][놀아볼래?][要玩吗]
歌词来源:http://music.163.com/#/song?id=22704479 作曲 : 赵英秀/김태현 [作曲 : 赵英秀/k/gim-Tae-hyeon] 作词 : 安英民 [作词 : ...
- Google C++ 编码规范
刚刚看到一位博主的文章分享Google C++ 编码规范 本人做一下记录,方便以后学习.. 中文在线版本地址: http://zh-google-styleguide.readthedocs.io/e ...
- PAT甲题题解-1073. Scientific Notation (20)-字符串处理
题意:给出科学计数法的格式的数字A,要求输出普通数字表示法,所有有效位都被保留,包括末尾的0. 分两种情况,一种E+,一种E-.具体情况具体分析╮(╯_╰)╭ #include <iostrea ...
- 《Linux内核分析》第三周:Linux系统启动过程
杨舒雯 原创作品转载请注明出处 Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.实验--使用gdb跟踪调试内 ...
- 圆桌的项目Alpha冲刺——测试
测试工作安排 作为一个测试计划来讲,核心的三个要素是时间,资源,范围.时间就是什么时候做以及要花多久做,资源就是你要调用的人力.机器等资源,范围是你要测试的东西以及测试重点. 时间:每天完成相应的模块 ...
- Alpha 冲刺三
团队成员 051601135 岳冠宇 051604103 陈思孝 031602629 刘意晗 031602248 郑智文 031602234 王淇 会议照片 项目燃尽图 项目进展 发布界面布局完成.登 ...