首页logo
  •  

jonllen

金龙,目前就职于一家软件公司,从事Java和.Net信息安全开发设计。

个人档案

jonllen
心情闪存 | 给他留言
妮称:jonllen
来自:中国. 湖南. 湘潭
简述:金龙,目前就职于一家软件公司,从事Java和.Net信息安全开发设计。
博客日历

loadScript和预载图片的JavaScript脚本

分类:JavaScript

加载自己的Ajax请求脚本文件

javascript:void(function(url,callback){var script=document.createElement('script');script.type='text/javascript';if(callback)script.onload=script.onreadystatechange=function(){if(script.readyState&&script.readyState!='loaded'&&script.readyState!='complete')return;script.onreadystatechange=script.onload=null;callback();};script.src=url;document.getElementsByTagName('head')[0].appendChild(script);}('http://www.hnonl.com/scripts/Ajax.js',function(){}))

使用Ajax发送请求

javascript:void(Ajax.send({type:"GET",url:"/",data:"parentId=1529640&blogId=57731&sourceUrl=/&title=document.getElementsByClassName的理想实现-Ruby'sLouvre-博客园&content=document.getElementsByClassName只有IE5不支持?如果要支持层次的筛选就好了,能象jquery那样$('div.class.item')那样来筛选,传一个参数就好了。&parentCommentId=0",fn:function(data){alert(data);},contentType:"application/x-www-form-urlencoded"}))

预载图片函数

function loadImg(src,width,height,callback,parms)
{
        
var imgElem = new Image();
        imgElem.style.display 
= "none";
        imgElem.onload 
= function (){
            
var loadWidth = parseFloat(imgElem.width), loadHeight = parseFloat(imgElem.height);
            
var zoomWidth = loadWidth, zoomHeight = loadHeight;
            
if(width && height && (loadWidth>width || loadHeight>height) )
            {
                
if(Math.abs( width-loadWidth) <  Math.abs( height-loadHeight) )
                {
                    
//宽度不变,等比缩放高度。
                    zoomWidth = width;
                    zoomHeight 
= (width*loadHeight)/loadWidth;
                    //高度大于需要高度,继续宽度缩放
                    if (zoomHeight > height)
                    {
                        zoomWidth 
= width*height/zoomHeight;
                        zoomHeight =  height;
                    }
                }
else
                {
                    zoomHeight 
= height;
                    zoomWidth 
= (height*loadWidth)/loadHeight;
                    if (zoomWidth>width)
                    {
                        zoomHeight 
= width*height/zoomWidth;
                        zoomWidth =  width;
                    }
                }
            }
            
if (callback) callback(src,zoomWidth,zoomHeight,parms);
            setTimeout(
function (){
                imgElem.parentNode.removeChild(imgElem);
                imgElem 
= null;
            },
0);
        };
        imgElem.src 
= src;
        imgElem.onabort 
= function (){ imgElem.src = imgElem.src; };
        
//imgElem.onreadystatechange=function(){ if (imgElem.readyState=="complete") {} }
        document.body.insertBefore(imgElem,document.body.childNodes[0]);
}
标签:JavaScript
  • posted@ 2009-07-24 10:51
  • update@ 2009-07-26 15:09:33
  • 阅读(11761)
  • 评论(0)

相关文章

评论
暂无任何评论。
发表评论
*必填
回复通知我
*必填