原生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 ...
随机推荐
- 记录:Ubuntu 18.04 安装 tensorflow-gpu 版本
狠下心来重新装了系统,探索一下 gpu 版本的安装.比较令人可喜的是,跟着前辈们的经验,还是让我给安装成功了.由于我是新装的系统,就像婴儿般纯净,所以进入系统的第一步就是安装 cuda,只要这个不出错 ...
- Jmeter(四)_16个逻辑控制器详解
循环控制器: 指定其子节点运行的次数,可以使用具体的数值,也可以设置为变量 1:勾选永远:表示一直循环下去 2:如果同时设置了线程组的循环次数和循环控制器的循环次数,那循环控制器的子节点运行的次数为两 ...
- C# List left join
public class Test1 { public int ID { get; set; } public string Name { get; set; } } public class Tes ...
- unity过场动画组件Timeline
Timeline是Unity2017版本中新加入的功能,可以非常方便的进行场景动画的创建和修改,包括物体.声音.粒子.动画.特效.自定义Playable以及子Timeline等多种资源进行整合,从而能 ...
- 【MAVEN】Missing artifact jdk.tools:jdk.tools:jar:1.6 eclipse
搭建开发环境,遇到问题 : IDE 使用 eclipse 公司的项目用Maven管理,从git上拿下来代码后开始build后: 提示 [missing artifact jdk.tools ...
- EOS开发基础之六:使用cleos命令行客户端操作EOS——智能合约之eosio.msig和eosio.system
上一节我们搞了老半天,也没整明白Exchange这个合约到底干啥用的.就它官方提供的说法,是用于货币的创造和交易.我没整明白,所以去看了下代码,发现代码头文件里面有下面这段话: This contra ...
- CMS漏洞检测工具 – CMSmap
CMSmap是一个Python编写的针对开源CMS(内容管理系统)的安全扫描器,它可以自动检测当前国外最流行的CMS的安全漏洞. CMSmap主要是在一个单一的工具集合了不同类型的CMS的常见的漏洞. ...
- Linux内核分析 笔记四 系统调用的三个层次 ——by王玥
一.知识点总结 (一)用户态.内核态和中断 1.内核态:在高的执行级别下,代码可以执行特权指令,访问任意的物理地址,这时的CPU就对应内核态 2.用户态:在低级别的指令状态下,代码 只能在级别允许的特 ...
- IOS的开发演变历史
对IOS开发平台一直抱有很大兴趣,正好通过这个机会好好了解一下IOS的开发历程! 通过一些查阅,我了解到IOS的开发平台主要是依靠Xcode软件来编写程序,同时又需要在MAC OS X的环境下运行,w ...
- 如何取消mysql的密码?
有两种方法: 1.mysql命令 SET PASSWORD FOR root@localhost=PASSWORD(''); 2.运行 mysqladmin 命令 mysqladmin -u roo ...