如何关闭微信内置浏览器

做微信开发中发现window.close失效,不能关闭浏览器,解决如下:

1
2
3
4
5
6
7
8
9
10
function closeWindow() {   
if (wx != undefined) {
wx.closeWindow();
}
else if (typeof(WeixinJSBridge) != "undefined") {
WeixinJSBridge.call('closeWindow');
} else {
window.close();
}
}

javascript函数作为变量传递

js中当函数不加括号时可以把函数当成值传递。

无参数:

1
2
3
4
5
6
// 无参函数
function foo (){
console.log(1);
}

var bar = foo;

有参时需要用一个匿名函数:

1
2
3
4
5
6
function foo (arg1, arg2) {
console.log(1);
}
var bar = function(arg1, arg2) {
foo(arg1, arg2);
};

让jquery插件支持AMD和CMD规范

jquery插件支持AMD和CMD规范的改造:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
;function(root, factory) {
"use strict";
if (typeof define === 'function' && (define.amd || define.cmd)) {

// register as anon module
define(['jquery'], factory);

} else {

// invoke directly
factory( (typeof(jQuery) != 'undefined') ? jQuery : window.Zepto );

}
}(this, function($){
'use strict';

// your code

});

javascript获取浏览器视口大小

1
2
3
4
5
6
7
8
9
10
11
12
13
function getViewport() {
if(document.compatMode == 'BackCompat'){
return {
width: document.body.clientWitdh,
height: document.body.clientHeight
}
} else {
return {
width: document.documentElement.clientWidth,
height: document.documentElement.clientHeight
}
}
}

php函数array_chunk的妙用

如果你想输出这么一个列表你会怎么做呢?

数据:

1
$cars=array("Volvo","BMW","Toyota","Honda","Mercedes","Opel");

最终结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
<section class="list">
<div>Volvo</div>
<div>BMW</div>
<br>
<div>Toyota</div>
<div>Honda</div>
<br>
<div>Mercedes</div>
<div>Opel</div>
<br>
...
</section>