Category Archives: js

javascript知识

javascript event兼容

event对象 IE 中可以直接使用 event 对象,而 FF 中则不可以,解决方法之一如下: var theEvent = window.event || arguments.callee.caller.arguments[0]; 第二种是将 event 作为参数来传递: function xxx(e){var theEvent = window.event || e;} srcElement 和 target 在 IE 中 srcElement 表示产生事件的源,比如是哪个按钮触发的 onclick 事件,FF 中则是 target。 var theEvent = window.event || … Continue reading

Posted in js | Leave a comment

修正IE6下PNG背景白色问题

IE6对PNG的图片唯一的缺陷就在于背景色如果是透明的话,会显示白色。可以使用下面的这段脚本来FIX。 var clear=”images/clear.gif” //path to clear.gif pngfix=function(){var els=document.getElementsByTagName(‘*’);var i_p=/\.png/i;var i=els.length;while (i– >0){var el=els[i];var es=el.style;if(el.src&&el.src.match(i_p)&&es.filter==”){el.height = el.height;el.width = el.width;es.filter = “progid:DXImageTransform.Microsoft.AlphaImageLoader(src=’”+el.src+”‘,sizingMethod=’crop’)”;el.src = clear;}else{var elb=el.currentStyle.backgroundImage;if(elb.match(i_p)){var path=elb.split(‘”‘);var rep=(el.currentStyle.backgroundRepeat==’no-repeat’)?’crop’:’scale’;es.filter=”progid:DXImageTransform.Microsoft.AlphaImageLoader(src=’”+path[1]+”‘,sizingMethod=’”+rep+”‘)”;es.height=el.clientHeight+’px’;es.backgroundImage=”none”;}}if (el.currentStyle.position!=’absolute’ && !es.filter && !el.tagName.match(/(body|html|script)/gi)) es.position=”relative”;if (es.filter&&el.currentStyle.position==”relative”) es.position=”static”;}} window.attachEvent(‘onload’,pngfix);

Posted in css, js | Leave a comment

jsachieve oriented object

/ //对象属性复制方法,很多库都有实现,如PrototypeJS里面的extend和Ext里面的Ext.apply // function extend(des, src) { if (!des) des = {}; if (src) { for (var i in src) { des[i] = src[i]; } } return des; } var CC = {}; //全局变量 // //create 用于创建类 // CC.create = … Continue reading

Posted in js | Leave a comment

js实现round

js中的自带ROUND函数只可以返回某一浮点数字的整数,而其他程序语言中的类似round函数都可以提供精确度参数,所以我们需要重写下js round函数。 function new_round(num,ext){ var t=1; var temp=ext; for(;temp>0;t*=10,temp–);//考虑小数点以后的数字 for(;temp<0;t/=10,temp++);//考虑整数部分 return Math.round(num*t)/t; };

Posted in js | Leave a comment

无法使用packer来压缩js代码

有些朋友使用packer来压缩JS时,会出现提示压缩后的代码语法不正确,缺少;,其实这主要的原因是packer需要所有的函数都以;结尾,比一般的要严格。 如: function hello(){alert(‘hello’)}; 具体原因见如下PACKER源代码: // remove: ;;; doSomething(); if ($this->_specialChars) $parser->add(‘/;;;[^\\n\\r]+[\\n\\r]/’); // remove redundant semi-colons $parser->add(‘/\\(;;\\)/’, self::IGNORE); // protect for (;;) loops $parser->add(‘/;+\\s*([};])/’, ‘$2′);

Posted in js | Leave a comment

HTML5新功能之显示拖入浏览器文件缩略图

演示地址(目前只支持FF火狐浏览器):http://www.w3cstudio.com/tools/html5/dragfile.html html5中的这个功能,很是强大,能获取到用户拖入到浏览器中文件的属性,这多亏与FILE对象,以及EVENT对象的dataTransfer属性,先给出源码: <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”> <html xmlns=”http://www.w3.org/1999/xhtml”> <head> <meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ /> <title>drage file into red div and see what happens</title> </head> <body> <input type=”file” id=”input” multiple=”true” onchange=”handleFiles(this.files)” /> <div id=”dropbox” style=”width:500px;height:200px;background-color:#900″>drag the file … Continue reading

Posted in js | Tagged , | Leave a comment

跨浏览器获取页面选择内容

function Selection(textareaElement) { this.element = textareaElement; } Selection.prototype.create = function() { if (document.selection != null && this.element.selectionStart == null) { return this._ieGetSelection(); } else { return this._mozillaGetSelection(); } } Selection.prototype._mozillaGetSelection = function() { return { start: this.element.selectionStart, end: this.element.selectionEnd }; … Continue reading

Posted in js | Leave a comment

js关闭网页提示

Onunload,onbeforeunload都是在刷新或关闭时调用,可以在脚本中通过window.onunload 来指定或者在里指定。区别在于onbeforeunload在onunload之前执行,它还可以阻止onunload的执行。   Onbeforeunload也是在页面刷新或关闭时调用,Onbeforeunload是正要去服务器读取新的页面时调用,此时还没开始读取;而onunload则已经从服务器上读到了需要加载的新的页面,在即将替换掉当前页面时调用。Onunload是无法阻止页面的更新和关闭的。而 Onbeforeunload 可以做到。 1、onbeforeunload事件:   说明:目前三大主流浏览器中firefox和IE都支持onbeforeunload事件,opera尚未支持。   用法:    ·object.onbeforeunload = handler    ·   描述:    事件触发的时候弹出一个有确定和取消的对话框,确定则离开页面,取消则继续待在本页。handler可以设一个返回值作为该对话框的显示文本。   触发于:    ·关闭浏览器窗口    ·通过地址栏或收藏夹前往其他页面的时候    ·点击返回,前进,刷新,主页其中一个的时候    ·点击 一个前往其他页面的url连接的时候    ·调用以下任意一个事件的时候:click,document write,document open,document close,window close ,window navigate ,window NavigateAndFind,location replace,location reload,form submit.    ·当用window open打开一个页面,并把本页的window的名字传给要打开的页面的时候。    ·重新赋予location.href的值的时候。    ·通过input type=”submit”按钮提交一个具有指定action的表单的时候。   可以用在以下元素:    ·BODY, FRAMESET, … Continue reading

Posted in js | Tagged | Leave a comment

国产版ADDTHIS jiathis源码分析

首先贴出,我破解的JS源码 // JavaScript Document – function() { var d = document, isStrict = d.compatMode == “CSS1Compat”, dd = d.documentElement, db = d.body, m = Math.max, ie = ! ! d.all, ua = navigator.userAgent.toLowerCase(), head = d.getElementsByTagName(‘head’)[0], getWH = function() { … Continue reading

Posted in js | 3 Comments

jsDom中节点类型

js在操作DOM时,可以获取到很多的NODE,如何判断当前的节点的类型,就可以使用节点的NODETYPE属性。 节点编号: 节点名称: 1 Element 2 Attribute 3 Text 4 CDATA Section 5 Entity Reference 6 Entity 7 Processing Instrucion 8 Comment 9 Document 10 Document Type 11 Document Fragment 12 Notation

Posted in js | Tagged , | Leave a comment