SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Preloading thumbs asking for advice and improvement

    Hi.
    This is my code:

    PHP Code:
    function preloadImg(url) {
            var 
    temp=new Image();
            
    temp.src=url;
        }
    function 
    imgs(){
            var 
    eldocument.getElementsByTagName('img');
            for(var 
    i=0,len=el.length;i<len;i++){
                
    urlel.item(i).src.replace('thumbs''uploaded');
                
    preloadImg(url);
                
    alert(url);
                
    addEvent(el.item(i),'click',popunder);
            }
        } 
    Any other better ways will be gracefully appreciated

    Bye.

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by whisher View Post
    Code:
    function preloadImg(url) {
    		var temp=new Image();
    		temp.src=url;
    	}
    preloadImg is called in a loop, meaning that temp is repeatedly created and destroyed before the download takes place.
    Tab-indentation is a crime against humanity.

  3. #3
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the point
    New version:
    PHP Code:
    function preloadImg(urls) {
        var 
    tmp=[];
        for(var 
    i=0,len=urls.length;i<len;i++){
            
    tmp[i]=new Image();
            
    tmp[i].src=urls[i];
        }
    }
    function 
    imgs(){
            var 
    urls= [];
            var 
    eldocument.getElementsByTagName('img');
            for(var 
    i=0,len=el.length;i<len;i++){
                
    urlel.item(i).src.replace('thumbs''uploaded');
                
    urls[i]=url;
                
    addEvent(el.item(i),'click',popunder);
            }
            
    preloadImg(urls);
        } 

    Bye.

  4. #4
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by whisher View Post
    Code:
    function preloadImg(urls) {
    	var tmp=[];
    	for(var i=0,len=urls.length;i<len;i++){
    		tmp[i]=new Image();
    		tmp[i].src=urls[i];
    	}
    }
    This doesn't really change anything. The function initialises a series of downloads, then immediately destroys the containing objects before the downloads can complete.
    Tab-indentation is a crime against humanity.

  5. #5
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, but what's your idea about a preload function ?

    Bye.

  6. #6
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In short:
    PHP Code:
    function Thumb(){
         var 
    imgs= {};
        function 
    preloadImg(){
            var 
    eldocument.getElementsByTagName('img');
            for(var 
    i=0,len=el.length;i<len;i++){
                var 
    tmpel.item(i);
                
    urltmp.src.replace('thumbs''uploaded');
                
    imgs[tmp.id]= new Image();
                
    imgs[tmp.id].src=url;
                
    imgs[tmp.id].title=tmp.title;
                
    imgs[tmp.id].alt=tmp.alt;
                
    addEvent(tmp,'click',popunder);
            }
        }
        function 
    box(largeImg){
            var 
    bodyEldocument.getElementsByTagName('body')[0];
            
    bodyEl.appendChild(largeImg);
        }
        function 
    popunder(e){
            
    cancelClick(e);
            var 
    imgTargetgetTarget(e);
            
    box(imgs[imgTarget.id]);
        }
        function 
    init(){
            if(!
    document.getElementById || !document.getElementsByTagName || !document.createTextNode){return;}
            
    preloadImg();
        }
        
    init();
    }
    addEvent(window,'load',Thumb); 
    My best


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •