原生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 ...
随机推荐
- Win10 + vs2017 编译并配置tesseract4.1.0
tesseract 是一个开源的OCR (Optical Character Recognition , 光学字符识别) 引擎,本文就介绍一下自己在编译 tesseract4.1.0时遇到的一些坑,希 ...
- A1043 Is It a Binary Search Tree (25 分)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- 1090. Highest Price in Supply Chain (25)-dfs求层数
给出一棵树,在树根出货物的价格为p,然后每往下一层,价格增加r%,求所有叶子节点中的最高价格,以及该层叶子结点个数. #include <iostream> #include <cs ...
- PAT甲题题解-1122. Hamiltonian Cycle (25)-判断路径是否是哈密顿回路
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789799.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- Alpha 贡献分及转会人员确定
贡献分如下: 转会人员:金东禾 转到队伍:bugphobia
- 《Linux内核分析》 第一节 计算机是如何工作的
第一节 计算机是如何工作的 张嘉琪 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-100002900 ...
- MFC Cstring转化为string
Cstring m_filePath; string sname( CW2A( m_filePath.GetString())); http://blog.sina.com.cn/s/blog_530 ...
- Ubuntu安装eclipse,并创建桌面快捷方式
系统:Ubuntu 16.04 JDK版本:1.8.0_121 Ubuntu下安装JDK配置环境变量可见我的这篇文章 http://www.cnblogs.com/AloneZ/p/Ubuntu1 ...
- 应对Gradle联网问题、长时间卡在resolve dependencies的思路
1.出现这种情况,在首先考虑网络问题,依赖下载不下来尝试shadowsocks,未果. 2.检查防火墙问题,更换host,无法解决. 3.新建Gradle工程,依然卡在resolve dependen ...
- 使用redis防止抢购商品超卖
前言: redis不仅仅是单纯的缓存,它还有一些特殊的功能,在一些特殊场景上很好用. 本篇博文用来测试下使用redis来防止抢购商品超卖问题. 内容: 使用redis的list进行测试 思路是设置一个 ...