User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 401,469 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,031 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 1334 | Replies: 8 | Solved
Reply
Join Date: Oct 2005
Posts: 236
Reputation: Inny is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 5
Inny's Avatar
Inny Inny is offline Offline
Posting Whiz in Training

Defer until load complete?

  #1  
May 13th, 2008
My script resizes images (yes, client side! No, I dont have access to do it server side!)

When a topic is posted with alot of images, The script sometimes resizes before all the images load, resulting in images loading after that which are not resized.

How can I ensure the script waits until ALL pics are loaded before resizing?

Or can I get the script to resize each image as It loads or something?
I just want to ensure they are ALL resized in the quickest way possible.


<script type='text/javascript'>
<!--
function ResizeThem(){
maxheight=300;
maxwidth= 300;
imgs=document.getElementsByTagName("img");
for (p=0; p<imgs.length; p++) {
if (imgs[p].getAttribute("alt")=="user posted image") {
w=parseInt(imgs[p].width);
h=parseInt(imgs[p].height);
if (parseInt(imgs[p].width)>maxwidth) {
imgs[p].style.cursor="pointer";
imgs[p].setAttribute('title','Reduced Image - Click to see full size');
imgs[p].onclick=new
Function('onclick=jkpopimage(this.src,600,500);');
imgs[p].height=(maxwidth/imgs[p].width)*imgs[p].height;
imgs[p].width=maxwidth;}
if (parseInt(imgs[p].height)>maxheight) {
imgs[p].style.cursor="pointer";
imgs[p].onclick=new
Function('onclick=jkpopimage(this.src,600,500);');
imgs[p].width=(maxheight/imgs[p].height)*imgs[p].width;
imgs[p].height=maxheight;}}}}
ResizeThem()
//-->
</script>

Always carry a flagon of whiskey in case of snakebite and furthermore always carry a small snake.
W. C. Fields
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2007
Location: Birmingham
Posts: 368
Reputation: Fungus1487 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 35
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Whiz

Re: Defer until load complete?

  #2  
May 13th, 2008
why not use the "defer" attribute of the script tag?

<script type='text/javascript' defer='defer'>

you could also add an onLoad event handler to run the script once the browser has loaded all information. But defer should work fine for what your after.
Last edited by Fungus1487 : May 13th, 2008 at 12:06 pm.
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Reply With Quote  
Join Date: Oct 2005
Posts: 236
Reputation: Inny is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 5
Inny's Avatar
Inny Inny is offline Offline
Posting Whiz in Training

Re: Defer until load complete?

  #3  
May 13th, 2008
defer didnt work, i tried that first; body onload works but is pretty slow
Last edited by Inny : May 13th, 2008 at 12:22 pm.
Always carry a flagon of whiskey in case of snakebite and furthermore always carry a small snake.
W. C. Fields
Reply With Quote  
Join Date: Apr 2007
Location: Birmingham
Posts: 368
Reputation: Fungus1487 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 35
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Whiz

Re: Defer until load complete?

  #4  
May 13th, 2008
i chopped up your code and used it with some big pics of my band.

here is the result resize example

my connection is fast but it seems to be resizing the image after it is loaded then moving on. But you would have to add an
onload="resizeImage(this)"
to every image tag you want resizing compared to your looping method. Personally i think the iterative loop at the end is a much better approach but this way works too.

var maxwidth = 100;
var maxheight = 100;
function resizeImage(img) {
	if(img.getAttribute("alt")=="user posted image") {
		w=parseInt(img.width);
		h=parseInt(img.height);
		if (parseInt(img.width)>maxwidth) {
			img.style.cursor="pointer";
			img.setAttribute('title','Reduced Image - Click to see full size');
			img.onclick=new
			Function('onclick=jkpopimage(this.src,600,500);');
			img.height=(maxwidth/img.width)*img.height;
			img.width=maxwidth;
		}
		if (parseInt(img.height)>maxheight) {
			img.style.cursor="pointer";
			img.onclick=new
			Function('onclick=jkpopimage(this.src,600,500);');
			img.width=(maxheight/img.height)*img.width;
			img.height=maxheight;
		}
	}
}

<p><img src="img.jpg" alt="user posted image" onload="resizeImage(this)" /></p>
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Reply With Quote  
Join Date: Oct 2005
Posts: 236
Reputation: Inny is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 5
Inny's Avatar
Inny Inny is offline Offline
Posting Whiz in Training

Re: Defer until load complete?

  #5  
May 13th, 2008
thanks mate, but adding to images just isnt an option im afraid, these are images posted by users with bbcode anyway, hundreds a day.
I appreciate your efforts!
I wonder if you could assign 'onload="resizeImage(this)' to them in the loop, in the same way the title attribute is?
Always carry a flagon of whiskey in case of snakebite and furthermore always carry a small snake.
W. C. Fields
Reply With Quote  
Join Date: Apr 2007
Location: Birmingham
Posts: 368
Reputation: Fungus1487 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 35
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Whiz

Re: Defer until load complete?

  #6  
May 13th, 2008
yes that may just work. but the increased time in doing this loop may take just as long as your original solution.
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Reply With Quote  
Join Date: Oct 2005
Posts: 236
Reputation: Inny is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 5
Inny's Avatar
Inny Inny is offline Offline
Posting Whiz in Training

Re: Defer until load complete?

  #7  
May 13th, 2008
just incidently your example gives 'object expected' error. dosent open enlarged
Always carry a flagon of whiskey in case of snakebite and furthermore always carry a small snake.
W. C. Fields
Reply With Quote  
Join Date: Apr 2007
Location: Birmingham
Posts: 368
Reputation: Fungus1487 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 35
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Whiz

Re: Defer until load complete?

  #8  
May 14th, 2008
is the function in the <head></head> area of the html document. the online example i put up works fine in both IE and firefox and the code has just been dragged from that.
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Reply With Quote  
Join Date: Oct 2005
Posts: 236
Reputation: Inny is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 5
Inny's Avatar
Inny Inny is offline Offline
Posting Whiz in Training

Re: Defer until load complete?

  #9  
May 14th, 2008
I meant your example above when i clicked on it, didnt open enlarged gave me that error in IE6, I didnt test it on my site.
I cant add tags to every image, Im sticking t my original script but calling it onload as you suggested.
Thanks for your Help mate!
Always carry a flagon of whiskey in case of snakebite and furthermore always carry a small snake.
W. C. Fields
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JavaScript / DHTML / AJAX Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 1:48 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC