分享10个原生JavaScript技巧
首先在这里要非常感谢无私分享作品的网友们,这些代码片段主要由网友们平时分享的作品代码里面和经常去逛网站然后查看源文件收集到的。把平时网站上常用的一些实用功能代码片段通通收集起来,方便网友们学习使用,利用好的话可以加快网友们的开发速度,提高工作效率。
1、原生JavaScript实现字符串长度截取
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
function cutstr(str, len) { var temp; var icount = 0; var patrn = /[^\x00-\xff]/; var strre = "" ; for ( var i = 0; i < str.length; i++) { if (icount < len - 1) { temp = str.substr(i, 1); if (patrn.exec(temp) == null ) { icount = icount + 1 } else { icount = icount + 2 } strre += temp } else { break } } return strre + "..." } |
2、原生JavaScript获取域名主机
1
2
3
4
5
6
7
8
9
10
11
12
|
function getHost(url) { var host = "null" ; if ( typeof url == "undefined" || null == url) { url = window.location.href; } var regex = /^\w+\:\/\/([^\/]*).*/; var match = url.match(regex); if ( typeof match != "undefined" && null != match) { host = match[1]; } return host; } |
3、原生JavaScript清除空格
1
2
3
4
|
String.prototype.trim = function () { var reExtraSpace = /^\s*(.*?)\s+$/; return this .replace(reExtraSpace, "$1" ) } |
4、原生JavaScript替换全部
1
2
3
|
String.prototype.replaceAll = function (s1, s2) { return this .replace( new RegExp(s1, "gm" ), s2) } |
5、原生JavaScript转义html标签
1
2
3
|
function HtmlEncode(text) { return text.replace(/&/g, '&' ).replace(/\"/g, '"' ).replace(/</g, '<' ).replace(/>/g, '>' ) } |
6、原生JavaScript还原html标签
1
2
3
|
function HtmlDecode(text) { return text.replace(/&/g, '&' ).replace(/"/g, '\"' ).replace(/</g, '<' ).replace(/>/g, '>' ) } |
7、原生JavaScript时间日期格式转换
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
Date.prototype.Format = function (formatStr) { var str = formatStr; var Week = [ '日' , '一' , '二' , '三' , '四' , '五' , '六' ]; str = str.replace(/yyyy|YYYY/, this .getFullYear()); str = str.replace(/yy|YY/, ( this .getYear() % 100) > 9 ? ( this .getYear() % 100).toString() : '0' + ( this .getYear() % 100)); str = str.replace(/MM/, ( this .getMonth() + 1) > 9 ? ( this .getMonth() + 1).toString() : '0' + ( this .getMonth() + 1)); str = str.replace(/M/g, ( this .getMonth() + 1)); str = str.replace(/w|W/g, Week[ this .getDay()]); str = str.replace(/dd|DD/, this .getDate() > 9 ? this .getDate().toString() : '0' + this .getDate()); str = str.replace(/d|D/g, this .getDate()); str = str.replace(/hh|HH/, this .getHours() > 9 ? this .getHours().toString() : '0' + this .getHours()); str = str.replace(/h|H/g, this .getHours()); str = str.replace(/mm/, this .getMinutes() > 9 ? this .getMinutes().toString() : '0' + this .getMinutes()); str = str.replace(/m/g, this .getMinutes()); str = str.replace(/ss|SS/, this .getSeconds() > 9 ? this .getSeconds().toString() : '0' + this .getSeconds()); str = str.replace(/s|S/g, this .getSeconds()); return str } |
8、原生JavaScript判断是否为数字类型
1
2
3
4
5
6
7
8
|
function isDigit(value) { var patrn = /^[0-9]*$/; if (patrn.exec(value) == null || value == "" ) { return false } else { return true } } |
9、原生JavaScript设置cookie值
1
2
3
4
5
6
7
8
9
|
function setCookie(name, value, Hours) { var d = new Date(); var offset = 8; var utc = d.getTime() + (d.getTimezoneOffset() * 60000); var nd = utc + (3600000 * offset); var exp = new Date(nd); exp.setTime(exp.getTime() + Hours * 60 * 60 * 1000); document.cookie = name + "=" + escape(value) + ";path=/;expires=" + exp.toGMTString() + ";domain=360doc.com;" } |
10、原生JavaScript获取cookie值
1
2
3
4
5
|
function getCookie(name) { var arr = document.cookie.match( new RegExp( "(^| )" + name + "=([^;]*)(;|$)" )); if (arr != null ) return unescape(arr[2]); return null } |
分享10个原生JavaScript技巧的更多相关文章
- 10个原生JavaScript技巧
这些代码片段主要由网友们平时分享的作品代码里面和经常去逛网站然后查看源文件收集到的.把平时网站上常用的一些实用功能代码片段通通收集起来,方便网友们学习使用,利用好的话可以加快网友们的开发速度,提高工作 ...
- 原生JavaScript技巧大收集(1~10)
1.原生JavaScript实现字符串长度截取 01 function cutstr(str, len) { 02 var temp; 03 var icount = 0; 04 ...
- 原生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面向对象设计小游戏
一.序言 不知大家是不是和我一样,当初都有个梦想.学编程,就是想开发游戏.结果进入大学学习之后,才知道搞的是数据库应用程序开发!在此,本人就分享下业余时间做的几个小游戏吧!本打算想用winform或w ...
- 原生JavaScript技巧
时常在技术论坛有看见一些比较好的示例,于是就出于一种收集并学习的态度,于是就保留下来啦~ 当然现在展示的也只是一部分,先放一部分出来尝尝鲜~~~
- 原生javascript学习
首先在这里要非常感谢无私分享作品的网友们,这些代码片段主要由网友们平时分享的作品代码里面和经常去逛网站然后查看源文件收集到的.把平时网站上常用的一些实用功能代码片段通通收集起来,方便网友们学习使用,利 ...
- 原生javascript写自己的运动库(匀速运动篇)
网上有很多JavaScript的运动库,这里和大家分享一下用原生JavaScript一步一步写一个运动函数的过程,如读者有更好的建议欢迎联系作者帮助优化完善代码.这个运动函数完成后,就可以用这个运动函 ...
随机推荐
- python练习笔记——分解质因数
分解质因数:输入一个正整数,分解质因数:如输入: 90 则打印: 90 = 2 * 3 * 3 * 5 get_str = input("请输入一个100以内的正整数,以分解质因数:&q ...
- 转: Python中的os.path.dirname(__file__)
(1).当"print os.path.dirname(__file__)"所在脚本是以完整路径被运行的, 那么将输出该脚本所在的完整路径,比如: ...
- RHCE7 管理II-6ACL的使用
ACL允许向文件分配细化的权限.除标准的文件所有者.组所有者.和其他文件权限之外,还可以指定用户或组,以及uid或guid确定的用户和组授予权限. 命令: ·setfacl 设置acl策略 ·getf ...
- 命名管道FIFO和mkfifo函数
进程间通信必须通过内核提供的通道,而且必须有一种办法在进程中标识内核提供的某个通道,前面讲过的匿名管道是用打开的文件描述符来标识的.如果要互相通信的几个进程没有从公共祖先那里继承文件描述符,它们怎么通 ...
- Linux中断 - GIC代码分析
一.前言 GIC(Generic Interrupt Controller)是ARM公司提供的一个通用的中断控制器,其architecture specification目前有四个版本,V1-V4(V ...
- linux查看与开启sshd服务
1.首先通过物理终端进入到linux上,手工检查ssh发现没运行/etc/init.d/sshd statussshd is stopped 手工启动服务,发现报告权限错误./etc/init.d/s ...
- react 事件绑定的2种常用方式
方式一:传统 import React, { Component } from 'react'; class App extends Component { handleSubmit (e, args ...
- 用ansible 完成一次性的工作(ad-Hoc)工作
ansible 真正强大的功能是它的playbook,但是在日常的工作中通过会遇到一些工作,它们只是需要我们偶尔操作一下:比较说重启一下 操作系统:像这样的工作就用不着ansible-playbook ...
- Linux安装mysql——源码安装
1.假设已经有mysql-5.5.10.tar.gz以及cmake-2.8.4.tar.gz两个源文件 (1)先安装cmake(mysql5.5以后是通过cmake来编译的) [root@ rhel5 ...
- 莫名其妙的js脚本文件引用不到
今天遇到一个很奇怪的问题,在页面中引用的脚本文件,引用路径没有问题,而且在很多浏览器中都测试没有问题,包括Win8 Modren UI下的IE11也同样没有问题,唯独Win8桌面版的IE11无法引用. ...