1、原生JavaScript实现字符串长度截取
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获取域名主机
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清除空格
String.prototype.trim = function() {
var reExtraSpace = /^\s*(.*?)\s+$/;
return this.replace(reExtraSpace, "$1")
}
4、原生JavaScript替换全部
String.prototype.replaceAll = function(s1, s2) {
return this.replace(new RegExp(s1, "gm"), s2)
}
5、原生JavaScript转义html标签
function HtmlEncode(text) {
return text.replace(/&/g, '&').replace(/\"/g, '"').replace(//g, '>')
}
6、原生JavaScript还原html标签
function HtmlDecode(text) {
return text.replace(/&/g, '&').replace(/"/g, '\"').replace(//g, '>')
}
7、原生JavaScript时间日期格式转换
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判断是否为数字类型
function isDigit(value) {
var patrn = /^[0-9]*$/;
if (patrn.exec(value) == null || value == "") {
return false
} else {
return true
}
}
9、原生JavaScript设置cookie值
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值
function getCookie(name) {
var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
if (arr != null) return unescape(arr[2]);
return null
相关推荐
原生JavaScript获取域名主机 function getHost(url) { var host = "null"; if(typeof url == "undefined"|| null == url) { url = [removed].href; } var regex = /^\w+\:\/\/([^\/]*).*/; var match = url....
原生JavaScript100例
基于原生JavaScript实现的扫雷小游戏 基于原生JavaScript实现的扫雷小游戏 基于原生JavaScript实现的扫雷小游戏 基于原生JavaScript实现的扫雷小游戏 基于原生JavaScript实现的扫雷小游戏 基于原生JavaScript实现的...
90、原生JavaScript常用的正则表达式大收集 91、原生JavaScript实现窗体改变事件resize的操作(兼容所以的浏览器) 92、原生JavaScript用正则清除空格分左右 93、原生JavaScript判断变量是否空值 94、原生JavaScript...
基于原生JavaScript完成的网页端2048游戏,适配移动端,绑定键盘和鼠标滑动事件 基于原生JavaScript完成的网页端2048游戏,适配移动端,绑定键盘和鼠标滑动事件 基于原生JavaScript完成的网页端2048游戏,适配移动端...
原生javascript页面图片懒加载效果
原生javascript实现ajax发送pos请求,这样可以脱离jquery框架,
自己用原生JavaScript写的一个购物车的网页,里面有一些简单的功能,根据数量计算价钱之类的
原生Javascript开发,让你的表单亮起来!
原生Javascript贪吃蛇代码
一些关于原生js小案例,如动画效果的实现,轮播图的做法,页面存储的属性等等
本项目是基于原生JavaScript的新闻发布门户及后台管理系统设计源码,包含162个文件,其中56个MAP文件,44个CSS文件,27个JS文件,13个HTML文件,12个SCSS文件,5个JPG文件,1个GITIGNORE文件,1个IML文件,1个XML...
AutoType 原生 JavaScript 实现的自动打字效果
利用原生JavaScript制作轮播图,对自己的阶段学习进行总结,欢迎大家下载!
javascript 原生代码全屏滚动 javascript 原生代码全屏滚动
原生JavaScript贪吃蛇完美实现 思路绝对让你眼前一亮!
原生javascript实现类似于excel的代码,我们可以再次开发这个插件让它集成后台功能,以及更多更强大的功能。
本文件属于文章《原生javascript表单验证实例教程》的完整代码,在读完原创文章之后可以下载查看代码。代码中有详细的注释说明。html、css、js代码都在同一个文件中。也可以供有需要的朋友在开发中直接使用。
javascript 原生tree
原生javascript实现的3D轮播动画,可扩展性强