Skip to content

Instantly share code, notes, and snippets.

View z2015's full-sized avatar
🎯
Focusing

William Zhou z2015

🎯
Focusing
View GitHub Profile
@z2015
z2015 / str-indexof.js
Created July 4, 2018 07:15
String IndexOf example
function strIndexOf(val, str) {
val = String(val);
var strLen = str.length;
if (strLen >= val.length) {
var head = val[0]
var hasHead = -1;
var matchLen = 0;
for (var i = 0; i < strLen; i++) {
var v = str[i];
@z2015
z2015 / remove.js
Created January 16, 2018 06:56
remove string comment and empty line
function remove(str){
var sCR = /(\/\/(.*)$)/gm;
var bCR = /(\/\*([\s\S]*?)\*\/)/gm;
bst eR = /^\s*[\r\n]/gm;
return str.replace(sCR, '')
.replace(bCR, '')
.replace(eR, '');
}
@z2015
z2015 / iframe-memory-leak-prevention.html
Created March 14, 2017 02:39 — forked from hallettj/iframe-memory-leak-prevention.html
iFrame memory leak prevention test
<!doctype html>
<html>
<head>
<title>iFrame memory leak prevention test</title>
</head>
<body>
<h1>iFrame memory leak prevention test (void)</h1>
<p>In IE up through version 8 adding an iframe to a page and
removing it produces a memory leak in some cases. The pattern
@z2015
z2015 / reg-greedy.md
Created August 31, 2016 06:06
Difference between .*? and .* for regex
@z2015
z2015 / ajaxPost.js
Created August 3, 2016 09:27
jQuery post json data
$.fn.serializeObject = function() {
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
@z2015
z2015 / bindnode.sh
Last active July 29, 2016 04:33
Nginx bind nodejs
server {
listen 80;
server_name wx.example.com;
location / {
proxy_pass http://localhost:1336;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
@z2015
z2015 / preventrefresh.js
Created July 21, 2016 08:28
Prevent automatic browser scroll on refresh
$(window).on('unload', function() {
$(window).scrollTop(0);
});
window.onunload = function(){ window.scrollTo(0,0); }
if ('scrollRestoration' in history) {
history.scrollRestoration = 'manual';
}
@z2015
z2015 / substr.js
Created July 19, 2016 01:31
Substr vs Substring
var status = '.status-0'
console.log(status.substr(2,2)); //first param by begin, second by cut length, got 'ta'
console.log($(this).attr('data-filter').substring(2,4)); // first param by begin index, second by sub index string. got 'ta' also .
@z2015
z2015 / isDuplicate.js
Created July 13, 2016 09:51
check if the array of objects have duplicate property values
var values = [
{ name: 'someName1' },
{ name: 'someName2' },
{ name: 'someName4' },
{ name: 'someName2' }
];
var valueArr = values.map(function(item){ return item.name });
var isDuplicate = valueArr.some(function(item, idx){
return valueArr.indexOf(item) != -1;
@z2015
z2015 / cache.md
Last active July 13, 2016 05:55
Cache-Control vs ETag

#Cache-Control适用场合 该参数用于缓存控制,在设置了该值后,浏览器会在超过该时间的情况下强行请求一次服务器,否则会一直使用上一次访问时的数据。 例如max-age=60,这里就告诉了浏览器在60秒内可以使用缓存,超过后需要重新向服务器发送请求。 这里要注意的事文件的访问方式局限于该文件是被其它文件所调用,或者是在新窗口访问该文件,如果在访问单个文件时使用了浏览器上的刷新按钮,那么就会浏览器就会强行触发一次发向服务器的请求。 这也就为什么很多人发现Cache-Control设置失效的原因。

#ETag的作用 该值一般是由服务器端生成的一段随机代码,用于指示当前访问文件的特殊值(文件的任何改变都会导致其改变)。一般用于校验当前文件是否是最新的文件。

#Cache-Control结合ETag的用法