操作页面刷新后,数据不能及时更新1+

23,982 views / 2009.09.01 / 6:06 下午

众所周知,刷新页面可以直接在html的head标签中使用如下代码:

<meta http-equiv="refresh" content="1; URL=http://www.71j.cn">

其中content中的数字1表示等待是时间(秒)

也可以使用php发送头消息:

header("location:http://71j.cn");

甚至可以使用js中window.location.reload()方法,但是我们在开发过程中发现,因为浏览器或者CDN等带有的缓存机制会使页面刷新后,数据得不到刷新。

解决办法如下:

function autorefresh(){
var s="&";
var href=window.location.href;
if(href.indexOf("t=")==-1){
if(href.indexOf("?")==-1) s="?";
window.location.href+=s+"t="+new Date().getTime();
}else{
window.location.href = href.replace(/t=[0-9]+/,"t="+new Date().getTime());
}
}

这样给url加上时间戳,可以有效解决本问题。

Categories: 感悟 Tags: , , ,

使用javascript从url获取参数值0+

5,291 views / 2009.09.01 / 6:06 下午

随着Ajax的泛滥,有时需要我们从url获取变量进行用户交互,本函数提供了获取方法:

function getQuery(name)
{
var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r!=null) return unescape(r[2]); return null;
}

如需要从下面地址中获取id的值:
http://71j.cn/a.php?a=my&id=7&m=1
只需要在当前页面调用getQuery(“id”)即可。

Categories: 感悟 Tags: , ,

我也收集了一些常用的正则表达式0+

13,575 views / 2009.09.01 / 5:05 下午

兄弟姊妹们,这可是极品文章,强烈建议大家收藏,以后不用每次都费劲脑汁编正则表达式了!
匹配中文字符的正则表达式: [\u4e00-\u9fa5]

匹配双字节字符(包括汉字在内):[^\x00-\xff]

应用:计算字符串的长度(一个双字节字符长度计2,ASCII字符计1)

String.prototype.len=function(){return this.replace([^\x00-\xff]/g,"aa").length;}

匹配空行的正则表达式:\n[\s| ]*\r

匹配HTML标记的正则表达式:/<(.*)>.*<\/\1>|<(.*) \/>/

匹配首尾空格的正则表达式:(^\s*)|(\s*$)

应用:javascript中没有像vbscript那样的trim函数,我们就可以利用这个表达式来实现,如下:

String.prototype.trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}

利用正则表达式分解和转换IP地址:

下面是利用正则表达式匹配IP地址,并将IP地址转换成对应数值的javascript程序:

function IP2V(ip)
{
re=/(\d+)\.(\d+)\.(\d+)\.(\d+)/g //匹配IP地址的正则表达式
if(re.test(ip))
{
return RegExp.$1*Math.pow(255,3))+RegExp.$2*Math.pow(255,2))+RegExp.$3*255+RegExp.$4*1
}
else
{
throw new Error("Not a valid IP address!")
}
}

不过上面的程序如果不用正则表达式,而直接用split函数来分解可能更简单,程序如下:

var ip="10.100.20.168"
ip=ip.split(".")
alert("IP值是:"+(ip[0]*255*255*255+ip[1]*255*255+ip[2]*255+ip[3]*1))

匹配Email地址的正则表达式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

匹配网址URL的正则表达式:http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?

利用正则表达式去除字串中重复的字符的算法程序:[注:此程序不正确,原因见本贴回复]

var s="abacabefgeeii"
var s1=s.replace(/(.).*\1/g,"$1")
var re=new RegExp("["+s1+"]","g")
var s2=s.replace(re,"")
alert(s1+s2) //结果为:abcefgi

我原来在CSDN上发贴寻求一个表达式来实现去除重复字符的方法,最终没有找到,这是我能想到的最简单的实现方法。思路是使用后向引用取出包括重复的字符,再以重复的字符建立第二个表达式,取到不重复的字符,两者串连。这个方法对于字符顺序有要求的字符串可能不适用。

得用正则表达式从URL地址中提取文件名的javascript程序,如下结果为page1

s="http://www.71j.cn/page1.htm"
s=s.replace(/(.*\/){0,}([^\.]+).*/ig,"$2")
alert(s)

利用正则表达式限制网页表单里的文本框输入内容:

用正则表达式限制只能输入中文:

onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))"

用正则表达式限制只能输入全角字符:

 onkeyup="value=value.replace(/[^\uFF00-\uFFFF]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\uFF00-\uFFFF]/g,''))"

用正则表达式限制只能输入数字:

onkeyup="value=value.replace(/[^\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"

用正则表达式限制只能输入数字和英文:

onkeyup="value=value.replace(/[\W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"

判断是否为数字

function isNumber(oNum)
{
if(!oNum) return false;
var strP=/^\d+(\.\d+)?$/;
if(!strP.test(oNum)) return false;
try{
if(parseFloat(oNum)!=oNum) return false;
}
catch(ex)
{
return false;
}
return true;
}
Categories: 分享 Tags: , ,

不容易找到的一张表 Content-Type大全0+

36,298 views / 2009.09.01 / 1:01 下午
描述 文件扩展名 MIME 类型
Text and Text-Related Types ? ? ?
HTML text data (RFC 1866) html htm ? text/html
Plain text: documents; program listings txt c c++ pl cc h ? text/plain
Richtext (obsolete – replaced by text/enriched) ? ? text/richtext
Structure enhanced text (etx?) ? text/x-setext
Enriched text markup (RFC 1896) ? ? text/enriched
Tab-separated values (tabular) (tsv?) ? text/tab-separated-values
SGML documents (RFC 1874) ? ? text/sgml
Speech synthesis data (MVP Solutions) talk ? text/x-speech
? ? ? ?
Document Stylesheet Types ? ? ?
Cascading Stylesheets css ? text/css
DSSSL-online stylesheets ? ? application/dsssl (proposed)
? ? ? ?
Image Types ? ? ?
GIF gif ? image/gif
X-Windows bitmap (b/w) xbm ? image/x-xbitmap
X-Windows pixelmap (8-bit color) xpm ? image/x-xpixmap
Portable Network Graphics png ? image/x-png
Image Exchange Format (RFC 1314) ief ? image/ief
JPEG jpeg jpg jpe ? image/jpeg
TIFF tiff tif ? image/tiff
RGB rgb ? image/rgb
? ? ? image/x-rgb
Group III Fax (RFC 1494) g3f ? image/g3fax
X Windowdump format xwd ? image/x-xwindowdump
Macintosh PICT format pict ? image/x-pict
PPM (UNIX PPM package) ppm ? image/x-portable-pixmap
PGM (UNIX PPM package) pgm ? image/x-portable-graymap
PBM (UNIX PPM package) pbm ? image/x-portable-bitmap
PNM (UNIX PPM package) pnm ? image/x-portable-anymap
Microsoft Windows bitmap bmp ? image/x-ms-bmp
CMU raster ras ? image/x-cmu-raster
Kodak Photo-CD pcd ? image/x-photo-cd
Computer Graphics Metafile cgm ? image/cgm
North Am. Presentation Layer Protocol ? ? image/naplps
CALS Type 1 or 2 mil cal ? image/x-cals
Fractal Image Format (Iterated Systems) fif ? image/fif
QuickSilver active image (Micrografx) dsf ? image/x-mgx-dsf
CMX vector image (Corel) cmx ? image/x-cmx
Wavelet-compressed (Summus) wi ? image/wavelet
AutoCad Drawing (SoftSource) dwg ? image/vnd.dwg
? ? ? image/x-dwg
AutoCad DXF file (SoftSource) dxf ? image/vnd.dxf
? ? ? image/x-dxf
Simple Vector Format (SoftSource) svf ? image/vnd.svf
? ? ? also vector/x-svf
? ? ? ?
Audio/Voice/Music Related Types ? ? ?
“basic”audio – 8-bit u-law PCM au snd ? audio/basic
Macintosh audio format (AIpple) aif aiff aifc ? audio/x-aiff
Microsoft audio wav ? audio/x-wav
MPEG audio mpa abs mpega ? audio/x-mpeg
MPEG-2 audio mp2a mpa2 ? audio/x-mpeg-2
compressed speech (Echo Speech Corp.) es ? audio/echospeech
Toolvox speech audio (Voxware) vox ? audio/voxware
RapidTransit compressed audio (Fast Man) lcc ? application/fastman
Realaudio (Progressive Networks) ra ram ? application/x-pn-realaudio
NIFF music notation data format ? ? application/vnd.music-niff
MIDI music data mmid ? x-music/x-midi
Koan music data (SSeyo) skp ? application/vnd.koan
? ? ? application/x-koan
Speech synthesis data (MVP Solutions) talk ? text/x-speech
? ? ? ?
Video Types ? ? ?
MPEG video mpeg mpg mpe ? video/mpeg
MPEG-2 video mpv2 mp2v ? video/mpeg-2
Macintosh Quicktime qt mov ? video/quicktime
Microsoft video avi ? video/x-msvideo
SGI Movie format movie ? video/x-sgi-movie
VDOlive streaming video (VDOnet) vdo ? video/vdo
Vivo streaming video (Vivo software) viv ? video/vnd.vivo
? ? ? video/vivo
Special HTTP/Web Application Types ? ? ?
Proxy autoconfiguration (Netscape browsers) pac ? application/x-ns-proxy-autoconfig
See Chapter 6 ? ? application/x-www-form-urlencoded
See Chapter 9 ? ? application/x-www-local-exec
See Chapter 9 (Netscape extension) ? ? multipart/x-mixed-replace
See Chapter 9 and Appendix B ? ? multipart/form-data
Netscape Cooltalk chat data (Netscape) ice ? x-conference/x-cooltalk
Interactive chat (Ichat) ? ? application/x-chat
? ? ? ?
Application Types ? ? ?
? ? ? ?
Text-Related ? ? ?
PostScript ai eps ps ? application/postscript
Microsoft Rich Text Format rtf ? application/rtf
Adobe Acrobat PDF pdf ? application/pdf
? ? ? application/x-pdf
Maker Interchange Format (FrameMaker) mif ? application/vnd.mif
? ? ? application/x-mif
Troff document t tr roff ? application/x-troff
Troff document with MAN macros man ? application/x-troff-man
Troff document with ME macros me ? application/x-troff-me
Troff document with MS macros ms ? application/x-troff-ms
LaTeX document latex ? application/x-latex
Tex/LateX document tex ? application/x-tex
GNU TexInfo document texinfo texi ? application/x-texinfo
TeX dvi format dvi ? application/x-dvi
MacWrite document ?? ? application/macwriteii
MS word document ?? ? application/msword
WordPerfect 5.1 document ?? ? application/wordperfect5.1
SGML application (RFC 1874) ? ? application/sgml
Office Document Architecture oda ? application/oda
Envoy Document evy ? application/envoy
Wang Info. Tranfer Format (Wang) ? ? application/wita
DEC Document Transfer Format (DEC) ? ? application/dec-dx
IBM Document Content Architecture (IBM) ? ? application/dca-rft
? ? ? ?
CommonGround Digital Paper (No Hands Software) ? ? application/commonground
FrameMaker Documents (Frame) doc fm frm frame ? application/vnd.framemaker
? ? ? application/x-framemaker
Remote printing at arbitrary printers (RFC 1486) ? ? application/remote-printing
? ? ? ?
Archive/Compressed Archives ? ? ?
Gnu tar format gtar ? application/x-gtar
4.3BSD tar format tar ? application/x-tar
POSIX tar format ustar ? application/x-ustar
Old CPIO format bcpio ? application/x-bcpio
POSIX CPIO format cpio ? application/x-cpio
UNIX sh shell archive shar ? application/x-shar
DOS/PC – Pkzipped archive zip ? application/zip
Macintosh Binhexed archive hqx ? application/mac-binhex40
Macintosh Stuffit Archive sit sea ? application/x-stuffit
Fractal Image Format fif ? application/fractals
Binary, UUencoded bin uu ? application/octet-stream
PC executable exe ? application/octet-stream
WAIS “sources” src wsrc ? application/x-wais-source
NCSA HDF data format hdf ? application/hdf
? ? ? ?
Downloadable Program/Scripts ? ? ?
Javascript program js ls mocha ? text/javascript
? ? ? application/x-javascript
VBScript program ? ? text/vbscript
UNIX bourne shell program sh ? application/x-sh
UNIX c-shell program csh ? application/x-csh
Perl program pl ? application/x-perl
Tcl (Tool Control Language) program tcl ? application/x-tcl
Atomicmail program scripts (obsolete) ? ? application/atomicmail
Slate documents – executable enclosures (BBN) ? ? application/slate
Undefined binary data (often executable progs) ? ? application/octet-stream
RISC OS Executable programs (ANT Limited) ? ? application/riscos
? ? ? ?
Animation/Multimedia ? ? ?
Andrew Toolkit inset ? ? application/andrew-inset
FutureSplash vector animation (FutureWave) spl ? application/futuresplash
mBED multimedia data (mBED) mbd ? application/mbedlet
Macromedia Shockwave (Macromedia) ? ? application/x-director
Sizzler real-time video/animation ? ? application/x-sprite
PowerMedia multimedia (RadMedia) rad ? application/x-rad-powermedia
? ? ? ?
Presentation ? ? ?
PowerPoint presentation (Microsoft) ppz ? application/mspowerpoint
PointPlus presentation data (Net Scene) css ? application/x-pointplus
ASAP WordPower (Software Publishing Corp.) asp ? application/x-asap
Astound Web Player multimedia data (GoldDisk) asn ? application/astound
Special Embedded Object ? ? ?
OLE script e.g. Visual Basic (Ncompass) axs ? application/x-olescript
OLE Object (Microsoft/NCompass) ods ? application/x-oleobject
OpenScape OLE/OCX objects (Business@Web) opp ? x-form/x-openscape
Visual Basic objects (Amara) wba ? application/x-webbasic
Specialized data entry forms (Alpha Software) frm ? application/x-alpha-form
client-server objects (Wayfarer Communications) wfx ? x-script/x-wfxclient
General Applications ? ? ?
Undefined binary data (often executable progs) ? ? application/octet-stream
CALS (U.S. D.O.D data format – RFC 1895) ? ? application/cals-1840
Pointcast news data (Pointcast) pcn ? application/x-pcn
Excel spreadsheet (Microsoft) ? ? application/vnd.ms-excel
? ? ? application/x-msexcel
? ? ? application/ms-excel
PowerPoint (Microsoft) ppt ? application/vnd.ms-powerpoint
? ? ? application/ms-powerpoint
Microsoft Project (Microsoft) ? ? application/vnd.ms-project
Works data (Microsoft) ? ? application/vnd.ms-works
MAPI data (Microsoft) ? ? application/vnd.ms-tnef
Artgallery data (Microsoft) ? ? application/vnd.artgalry
SourceView document (Dataware Electronics) svd ? application/vnd.svd
Truedoc (Bitstream) ? ? application/vnd.truedoc
Net Install – software install (20/20 Software) ins ? application/x-net-install
Carbon Copy – remote control/access (Microcom) ccv ? application/ccv
Spreadsheets (Visual Components) vts ? workbook/formulaone
Cybercash digital money (Cybercash) ? ? application/cybercash
Format for sending generic Macintosh files ? ? application/applefile
Active message — connect to active mail app. ? ? application/activemessage
X.400 mail message body part (RFC 1494) ? ? application/x400-bp
USENET news message id (RFC 1036) ? ? application/news-message-id
USENET news message (RFC 1036) ? ? application/news-transmission
? ? ? ?
Multipart Types (mostly email) ? ? ?
Messages with multiple parts ? ? multipart/mixed
Messages with multiple, alternative parts ? ? multipart/alternative
Message with multiple, related parts ? ? multipart/related
Multiple parts are digests ? ? multipart/digest
For reporting of email status (admin.) ? ? multipart/report
Order of parts does not matter ? ? multipart/parallel
Macintosh file data ? ? multipart/appledouble
Aggregate messages; descriptor as header ? ? multipart/header-set
Container for voice-mail ? ? multipart/voice-message
HTML FORM data (see Ch. 9 and App. B) ? ? multipart/form-data
Infinite multiparts – See Chapter 9 (Netscape) ? ? multipart/x-mixed-replace
? ? ? ?
Message Types (mostly email) ? ? ?
MIME message ? ? message/rfc822
Partial message ? ? message/partial
Message containing external references ? ? message/external-body
Message containing USENET news ? ? message/news
HTTP message ? ? message/http
? ? ? ?
2D/3D Data/Virtual Reality Types ? ? ?
VRML data file wrl vrml ? x-world/x-vrml(changing to model/vrml)
WIRL – VRML data (VREAM) vrw ? x-world/x-vream
Play3D 3d scene data (Play3D) p3d ? application/x-p3d
Viscape Interactive 3d world data (Superscape) svr ? x-world/x-svr
WebActive 3d data (Plastic Thought) wvr ? x-world/x-wvr
QuickDraw3D scene data (Apple) 3dmf ? x-world/x-3dmf
? ? ? ?
Scientific/Math/CAD Types ? ? ?
Chemical types — to communicate information about chemical models ? ? chemical/* (several subtypes)
Mathematica notebook ma ? application/mathematica
Computational meshes for numerical simulations msh ? x-model/x-mesh(evolving to model/mesh)
Vis5D 5-dimensional data v5d ? application/vis5d
IGES models — CAD/CAM (CGM) data igs ? application/iges(evolving to model/iges?)
Autocad WHIP vector drawings dwf ? drawing/x-dwf
? ? ? ?
Largely Platform-Specific Types ? ? ?
Silicon Graphics Specific Types ? ? ?
Showcase Presentations showcase slides sc sho show ? application/x-showcase
Insight Manual pages ins insight ? application/x-insight
Iris Annotator data ano ? application/x-annotator
Directory Viewer dir ? application/x-dirview
Software License lic ? application/x-enterlicense
Fax manager file faxmgr ? application/x-fax-manager
Fax job data file faxmgrjob ? application/x-fax-manager-job
IconBook data icnbk ? application/x-iconbook
? wb ? application/x-inpview
Installable software in ‘inst’ format inst ? application/x-install
Mail folder mail ? application/x-mailfolder
? pp ppages ? application/x-ppages
Data for printer (via lpr) sgi-lpr ? application/x-sgi-lpr
Software in ‘tardist’ format tardist ? application/x-tardist
Software in compressed ‘tardist’ format ztardist ? application/x-ztardist
WingZ spreadsheet wkz ? application/x-wingz
Open Inventor 3-D scenes iv ? graphics/x-inventor
? ? ? ?

来源:http://www.utoronto.ca/webdocs/HTMLdocs/Book/Book-3ed/appb/mimetype.html

Categories: 分享 Tags:

CDN缓存策略FAQ0+

12,311 views / 2009.09.01 / 12:12 下午

1.CDN加速原理
通过动态域名解析,网友的请求被分配到离自己最快的服务器。CDN服务器直接返回缓存文件或通过专线代理原站的内容。
网络加速+内容缓存,有效提供访问速度

2.CDN节点数量
全国多个机房,每个机房多台服务器,CDN节点一般上百台

3.CDN缓存什么内容
缓存html、图片、css、xml等静态资源,不缓存含有?的动态地址、jsp、php,js文件也不缓存【除非特殊设置】
缓存原站返回HTTP状态为20*或304,不缓存其他状态(例如404,500,503)

4.CDN缓存内容的更新
a)用户首次请求,CDN从原站抓取后缓存,直到文件过期后有用户请求再次更新
b)程序主动通知CDN抓取

5.CDN缓存内容的有效期
a)原站apache吐出的静态文件:由apache的expire和header模块控制
主要两项:last-modified,cache-control:max-age
apache缺省配置,所有静态文件在cdn只缓存3600s【需要我们按需求调整被加速服务器的apache设置】
3600s后cdn失效,用户访问时会重新请求原站,如果没有变化,缓存失效周期自动延长10%。
b)原站jsp或php吐出的动态内容(url形式必须是静态的)
由程序控制last-modified,cache-control:max-age public ,apache的设置将不起作用
cdn根据这两项判断是否需要到原站更新内容

6.CDN和应用的结合策略
a)变化不频繁的页面:例如图吧的图片显示页、车型页、已结束的比赛对阵页
在原站生成静态页面,原站apache上定义过期时间,例如1天。
原站上静态文件更新后,可以等待cdn过期。或者主动通知cdn更新(随着cdn节点越来越多,代价会非常高)
b)变化频繁的页面:例如足球库中的及时亚盘、及时欧赔、正在进行的比赛对阵页
不生成静态页面,由jsp或php定义过期时间,例如5s或60s。cdn过期后,如果有用户访问就从原站上抓取。
优点:相关页面内容更新后,不需要主动通知100个原站都来抓取,有效降低原站的压力。
如果页面内容没有变化,返回lastmodified不变,这样原站会直接返回304给cdn,cdn也会返回304给用户。减少网络传输和速度
比赛结束后,“正在进行的比赛对阵页”转换为第一类情况,再生成静态文件
c)特殊静态资源:例如图片库和某些大型产品库中的评论js
或者频繁访问、频繁更新的页面:例如足球赛事库的及时比分文件
通过apache nocache告诉IE不缓存,html中就不需要使用pinglun.js?123456这样的代码形式
然后用max-age告诉cdn缓存1s,这样避免每次用户请求都转到原站

Categories: 感悟 Tags: , ,