JavaScript纯符号输出文本

最近发现一种新的玩法,一篇纯符号的就能显示出各种各样的文本,有点像C语言的乱码大赛的那种玩法。

感觉很感兴趣,具体研究了一下,是因为js的语言特性,可以说是它的“劣势”:

很奇怪的特性就在于,很多特殊语法拼凑起来就成了一种违反直觉的结果类型。纯符号就建立在这样的机制上面。下面我们来详细解析:
继续阅读JavaScript纯符号输出文本

js前台获取URL参数


Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /www/wwwroot/fawdlstty.com/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /www/wwwroot/fawdlstty.com/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

对于网页前台,有时候是需要获取网页参数的,比如https://www.fawdlstty.com/?p=57,js如何获取参数呢?这一个参数还好,如果参数多了那么解析就费事了。下面提供一种方法实现最便捷的方式获取网页参数。实现代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
function getRequest() {
	var url = location.search;
	var obj = {};
	if (url.indexOf('?') != -1) {
		url = url.substring(url.indexOf('?') + 1).split('&');
		for (var i = 0; i < url.length; i++) {
			var strs = url[i].split('=');
			obj[strs[0]] = unescape(strs[1]);
		}
	}
	return obj;
}

代码量很小,使用方式如下,比如,获取参数名为p的值:

1
2
var param = getRequest();
alert(param['p']);

调用也是相当方便呢

ASP.Net前台提交Post后,后台Session为空的解决

ASP.Net会在WebForm中加入一两个隐藏的参数(有时候是一个有时候是两个),用于提交form表单时识别Session,如下图所示:
vierstate
然而直接通过$.post()向后台提交信息,后台是得不到这两个参数的,故而无法识别Session,从而导致Session为空。除此之外,前台提交new FormData()生成的表单,后台也无法识别。解决方法是直接在Post参数中加入这两个临时变量即可。
继续阅读ASP.Net前台提交Post后,后台Session为空的解决

让jQuery.css()方法支持important属性


Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /www/wwwroot/fawdlstty.com/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /www/wwwroot/fawdlstty.com/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

jQuery的css()方法支持直接设置元素的element,真是方便了你我他,美中不足的是不支持important属性。网上的一些高手就出了对策,下面转载一个个人认为相当不错的实现。
来源:http://stackoverflow.com/questions/2655925/how-to-apply-important-using-css

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
(function($) {    
	if ($.fn.style) {
		return;
	}
 
	// Escape regex chars with \
	var escape = function(text) {
		return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
	};
 
	// For those who need them (< IE 9), add support for CSS functions
	var isStyleFuncSupported = !!CSSStyleDeclaration.prototype.getPropertyValue;
	if (!isStyleFuncSupported) {
		CSSStyleDeclaration.prototype.getPropertyValue = function(a) {
			return this.getAttribute(a);
		};
		CSSStyleDeclaration.prototype.setProperty = function(styleName, value, priority) {
			this.setAttribute(styleName, value);
			var priority = typeof priority != 'undefined' ? priority : '';
			if (priority != '') {
				// Add priority manually
				var rule = new RegExp(escape(styleName) + '\\s*:\\s*' + escape(value) + '(\\s*;)?', 'gmi');
				this.cssText = this.cssText.replace(rule, styleName + ': ' + value + ' !' + priority + ';');
			}
		};
		CSSStyleDeclaration.prototype.removeProperty = function(a) {
			return this.removeAttribute(a);
		};
		CSSStyleDeclaration.prototype.getPropertyPriority = function(styleName) {
			var rule = new RegExp(escape(styleName) + '\\s*:\\s*[^\\s]*\\s*!important(\\s*;)?', 'gmi');
			return rule.test(this.cssText) ? 'important' : '';
		}
	}
 
	// The style function
	$.fn.style = function(styleName, value, priority) {
		// DOM node
		var node = this.get(0);
		// Ensure we have a DOM node
		if (typeof node == 'undefined') {
			return this;
		}
		// CSSStyleDeclaration
		var style = this.get(0).style;
		// Getter/Setter
		if (typeof styleName != 'undefined') {
			if (typeof value != 'undefined') {
				// Set style property
				priority = typeof priority != 'undefined' ? priority : '';
				style.setProperty(styleName, value, priority);
				return this;
			} else {
				// Get style property
				return style.getPropertyValue(styleName);
			}
		} else {
			// Get CSSStyleDeclaration
			return style;
		}
	};
})(jQuery);

调用方式:

1
2
3
4
5
6
7
8
9
$('#id').style('display', 'block !important');
//上面是我认为最好理解的调用方式,下面是作者给出的调用方式
var div = $('someDiv');
console.log(div.style('color'));
div.style('color', 'red');
console.log(div.style('color'));
div.style('color', 'blue', 'important');
console.log(div.style('color'));
console.log(div.style().getPropertyPriority('color'));

js Array扩展方法


Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /www/wwwroot/fawdlstty.com/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

js的Array对象的处理能力比较强大,下面几个函数是给它再新增几个扩展方法使其更方便使用
部分方法在最新版Webkit已经实现,但由于IE8等低端浏览器还占有大批用户,所以还得实现一遍,另外splice不是很方便使用,个人感觉用insert、remove方法更直观

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//数组处理程序
if (!Array.prototype.forEach) {
	Array.prototype.forEach = function (callback, thisArg) {
		var T, k;
		if (this == null) {
			throw new TypeError(" this is null or not defined");
		}
		var O = Object(this);
		var len = O.length >>> 0; // Hack to convert O.length to a UInt32  
		if ({}.toString.call(callback) != "[object Function]") {
			throw new TypeError(callback + " is not a function");
		}
		if (thisArg) {
			T = thisArg;
		}
		k = 0;
		while (k < len) {
			var kValue;
			if (k in O) {
				kValue = O[k];
				callback.call(T, kValue, k, O);
			}
			k++;
		}
	};
}
 
//查询数组中是否存在元素
if (!Array.prototype.in_array) {
	Array.prototype.in_array = function (e) {
		for (i = 0; i < this.length; i++) {
			if (this[i] == e)
				return true;
		}
		return false;
	}
}
 
//插入元素
if (!Array.prototype.insert) {
	Array.prototype.insert = function (index, elem) {
		return this.splice(index, 0, elem);
	}
}
 
//删除元素
if (!Array.prototype.remove) {
	Array.prototype.remove = function (index) {
		return this.splice(index, 1);
	}
}

js日期格式化的实现


Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /www/wwwroot/fawdlstty.com/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

功能比较简单,一个函数实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
if (!Date.prototype.Format) {
	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() > 9 ? this.getMonth().toString() : '0' + this.getMonth());
		str = str.replace(/M/g, this.getMonth());
		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;
	}
}

这里给Date对象增加一个Format属性,调用方法示例:date.Format('yyyy-MM-dd');