Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
(PrototypeJS) AJAX Request show loading till all images are loaded
Old 08-15-2009, 06:27 AM (PrototypeJS) AJAX Request show loading till all images are loaded
Novice Talker

Posts: 8
Name: Jozo
Trades: 0
Hi all,

I'm working on a site wich uses the Prototye framework to load portfolio gallererys, wich have a lot of images, so I want that the script keeps showing Loading..." till all the images are loaded.
The built-in "onSucces" only waits for the HTML, and not the images.
On the Prototype site, I found that "window.onload" might be the event I need.

But I really don't know how to implement this.

This is my current script, it works, but it doesnt wait till all the images are loaded:
Code:
function loadgallery(id){
  $$('.contentcontainer').invoke('hide');
  $('gallery').hide();
  $('loading').appear({duration: 0.3});
  var url = 'gallery.php';
  var params = 'id=' + id;
  new Ajax.Request(
  url,
    { method: 'get', parameters: params, onComplete: function(transport){
      $('loading').hide();
      $('gallery').appear({duration: 0.3});
      $('gallery').innerHTML = transport.responseText;
    }}
  );
 
  document.stopObserving(window, 'load');
}
Greets from Holland!
Appelmoes is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-15-2009, 06:41 AM Re: (PrototypeJS) AJAX Request show loading till all images are loaded
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,528
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
That's because the script doesn't and can't "know" when the images are loaded.

HTTP is a disconnected protocol, there is no communication between client and server beyond the client requesting the URI from the server.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-15-2009, 07:29 AM Re: (PrototypeJS) AJAX Request show loading till all images are loaded
Novice Talker

Posts: 8
Name: Jozo
Trades: 0
But i'm sure there must be a way. Maybe adding code in the gallery.php file(wich is being requested by ajax).

I guesst you're right that Ajax.Request can't know when the images are loaded, but i'm sure JavaScript knows it. I just don't know how to implement this.
Any suggestions?
Appelmoes is offline
Reply With Quote
View Public Profile
 
Old 08-15-2009, 07:41 AM Re: (PrototypeJS) AJAX Request show loading till all images are loaded
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,528
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
javascript has NO access to the client machine HD so cannot check if an image is cached/loaded.
PHP has NO ACCESS to the client machine FULL STOP!
The webserver only "knows" the file was requested.
Javascript has NO METHODS of hooking into the brower functions other than events being raised.
There are no events raised when images are fully cached.

SO unless you are going to write an ActiveX component (IE only) and install it on every client machine (security implications with this)
It is not going to be possible.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-15-2009, 08:39 AM Re: (PrototypeJS) AJAX Request show loading till all images are loaded
Novice Talker

Posts: 8
Name: Jozo
Trades: 0
Yes JS cannot comminucate with OS of course, but it can communicate with the browser! And there is an event where the browser tells "this page is fully loaded".
There is an event wich the browser fires *right after* all images are loaded.

Read this:
"If you used window.onload before you might want to switch to dom:loaded because it will fire immediately after the HTML document is fully loaded, but before images on the page are fully loaded. The load event on window only fires after all page images are loaded, making it unsuitable for some initialization purposes like hiding page elements (so they can be shown later)."
Source: Prototype API docs.

PS: your signature links dont seem to work!


EDIT: I made a simple script to show you what i mean, and to show that it is possible with javascript. Now i just want to implement this into my ajax website.
here: http://joelmeijering.byethost6.com/debora/test/

Last edited by Appelmoes; 08-15-2009 at 08:59 AM..
Appelmoes is offline
Reply With Quote
View Public Profile
 
Old 08-15-2009, 10:34 AM Re: (PrototypeJS) AJAX Request show loading till all images are loaded
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,528
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
yep, there is a onload event for the body element and every page element (including images) can have an onload event associated with ot. But there isn't a standard means to know when ALL images are fully loaded.

You could of course add a listener to all the img onload events if you so wished or you could add a preload script.

JS can only communicate with the browser through the standard DOM interface events and methods which are limited in their scope (deliberately) by default.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-15-2009, 10:50 AM Re: (PrototypeJS) AJAX Request show loading till all images are loaded
Novice Talker

Posts: 8
Name: Jozo
Trades: 0
Yes, OK, but how do i inplement this any ideas?
The problem is that i'm kinda beginner and almost all things I dpo s are found with google, and there are a lot of things wich I learned that way(so i dont need google anymore for those things). But the problem is that i can't find a solution on google for this, thats why I'm here. So could you or anybody else give me a push in the right direction?
(please don't mind my bad English)
Appelmoes is offline
Reply With Quote
View Public Profile
 
Old 08-15-2009, 11:40 AM Re: (PrototypeJS) AJAX Request show loading till all images are loaded
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,528
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
No idea how it would be done with the framework you are using.
But I would assume it had listeners and event handlers builtin that you could create, instantiate and assign to the various elements on your page.

Here is an image preloader at TechRepublic that I dredged up from my bookmarks.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-15-2009, 11:54 AM Re: (PrototypeJS) AJAX Request show loading till all images are loaded
Novice Talker

Posts: 8
Name: Jozo
Trades: 0
OK, ill look at it.
But do you think its possible with javascript to first load the images with ajax and then show them when there all loaded?

And another question, instead of watching all the images, i could maybe just watch(/observe) the last one? Because i really dont know how to put them in an array and abserve them all.


Look, its really hot now here in holland, and i wish that i could swim but... i have 5 adhesions in my feet

Last edited by Appelmoes; 08-15-2009 at 11:58 AM..
Appelmoes is offline
Reply With Quote
View Public Profile
 
Old 08-15-2009, 01:04 PM Re: (PrototypeJS) AJAX Request show loading till all images are loaded
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,528
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
How would you know which was the last one? because they don't necessarily load in the order they are in the document

document.images will return an array of all the images in the document.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-15-2009, 01:06 PM Re: (PrototypeJS) AJAX Request show loading till all images are loaded
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,528
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
BTW I'd probably write something up but I'm just killing time while loading up a new server and wating for things to happen
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-15-2009, 01:52 PM Re: (PrototypeJS) AJAX Request show loading till all images are loaded
Novice Talker

Posts: 8
Name: Jozo
Trades: 0
Ok, ill make a sketch please say if you think its gonna work:

Code:
function loadgallery(id){
- hide/show some divs

- do the ajax request, update the hidden div with the content

(images are loading but not visual...)
...

document.images.each("function(){
 wait for the image to load, then go to next image
}")

after loop has finished:
show the div and hide the loading div

done!

}
Do you think this can work?
Appelmoes is offline
Reply With Quote
View Public Profile
 
Old 08-16-2009, 06:04 PM Re: (PrototypeJS) AJAX Request show loading till all images are loaded
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,528
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
You could play with this code
Code:
var m_oaImage = Array();

	function setImage(id) {
		this.id = id;
		this.name  = id;
		this.loaded = false;
		
	}
	
	
function imgLoaded(name) {
	m_oaImage.name.loaded = true;
}
function getImages() {
	images = document.images;
	for (i=0; i<images.length; i++) {
		m_oaImage[i] = new setImage(images[i].id);
		images[i].onload = function(){imgLoaded(this.id)};
	}		
}
threw it together earlier, haven't actually tested it to see if it does what it should though.

The script creates an array of the images and as each image loads it will set m_oaImage[index].loaded to true. So in theory you could loop through the array and check for each loaded property being true.
HOWEVER it may not work as hoped because the onload event is triggered when the element loads NOT necessarily when the content loads and renders.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-17-2009, 06:32 AM Re: (PrototypeJS) AJAX Request show loading till all images are loaded
Novice Talker

Posts: 8
Name: Jozo
Trades: 0
Thanks, but maybe the event is only triggered for the first time you load the site, and the second time it doesnt have to trigger onload becouse the images are already downloaded and in cache.
And im ****ed up because i found out that some browsers dont load "display: none". So my whole **** wont work then.
So i did al lot of work for nothing i think and maybe im going to make it just basic like any other site(so no ajax javascript **** anaymore)

But i dont realyy understand your script, when does it know that all the images are loaded'?

cyja

Last edited by Appelmoes; 08-17-2009 at 06:34 AM..
Appelmoes is offline
Reply With Quote
View Public Profile
 
Old 08-17-2009, 07:33 AM Re: (PrototypeJS) AJAX Request show loading till all images are loaded
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,528
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Quote:
Thanks, but maybe the event is only triggered for the first time you load the site, and the second time it doesnt have to trigger onload becouse the images are already downloaded and in cache.
Nope. The onload event is triggered WHEN THE ELEMENT LOADS NOT when the image is downloaded.

Quote:
But i dont realyy understand your script, when does it know that all the images are loaded'
When all the .loaded values are true.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-18-2009, 02:06 PM Re: (PrototypeJS) AJAX Request show loading till all images are loaded
Novice Talker

Posts: 8
Name: Jozo
Trades: 0
Thanks, ill try it when i have time.
Maybe i'd better first completely build the site and then build all the fancy js ajax etc stuff.
But i guess thats to late for now

Last edited by Appelmoes; 08-18-2009 at 02:43 PM..
Appelmoes is offline
Reply With Quote
View Public Profile
 
Old 10-20-2009, 09:57 AM Re: (PrototypeJS) AJAX Request show loading till all images are loaded
Junior Talker

Posts: 1
Name: Aaron Reynolds
Location: Manchester, UK
Trades: 0
If you are concerned about the images not loading because they are hidden, why not put them inside a DIV that's positioned above the page - so it can't be seen, e.g.:

<div style="position: absolute; top: -1000px;">
<img
__________________
Hobbies - what hobbies? I'm on here aren't I !


Please login or register to view this content. Registration is FREE
TheA98 is offline
Reply With Quote
View Public Profile Visit TheA98's homepage!
 
Reply     « Reply to (PrototypeJS) AJAX Request show loading till all images are loaded
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.50132 seconds with 12 queries