473,405 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

hiding image until src updated

35
Hello,

I'm having a site where, at the page load, I'm updating the src of an image using an Ajax call, and then using the response to set the new src attribute.

The problem is, the page finishes loading before my images have been updated (or fully loaded). This means the images first show up incorrectly for some milliseconds, before changing to their correct image after the src has been updated.

I'm triggering the image action on window.onload. I'm actually doing this now:

Expand|Select|Wrap|Line Numbers
  1. function init() {
  2.     hideImages(); // hide all images on site
  3.     checkImages(); // modifies the src of every image
  4.     showImages(); // shows all the images again
  5.     document.getElementById('main').style.display = "block"; // makes content visible since it's got display:none by default
  6. }
  7. window.onload = init;
How should I do this?

Thanks!
Jan 18 '10 #1
7 1372
If I understand you correctly, all you need to do is put a delay of a few milliseconds before you call showImages(), as the images are being shown before they can be updated with the new source. Test your site on a slow connection as these can be slower to update, to ensure you have set your timer to the correct length.
Jan 18 '10 #2
gits
5,390 Expert Mod 4TB
a delay is not really nice nor is it really reliable ... have a look at the image's onload handler ... which can be used for such purposes ... with that you have an event on which you could trigger the init-function.

kind regards
Jan 19 '10 #3
True about the delay. I'm not sure if onload will work, though - I mean, after all, the image already HAS been loaded! If onload does work, I'll be very interested. If not, try the delay. You probably won't need more than a few millisecs anyway, which won't really make much of a difference - even for users with dial-up.
Jan 19 '10 #4
gits
5,390 Expert Mod 4TB
by changing the src-attribute a async request to the 'new' ressource is made in any case ... onload is fired when this request is finished ... so it should work when the flow of all the code is correctly arranged. delays should never be used to do things that could have a defined state but currently don't. delays should only be used in cases where they really make sense ... like delaying the call of a function in a suggest-field so that the user might type in something and the function/request is not started on every single keystroke - or similar cases. So the basic rule of thumb is to avoid delays wherever you can since it often is just a beginner workaround instead of fixing the code flow correctly.
Jan 19 '10 #5
Wow. So doesn't this mean that if I load a page into a div using AJAX an onload event would be fired for that request? Why do people use onReadyStateChange then?
Jan 19 '10 #6
gits
5,390 Expert Mod 4TB
images cannot be transfered with an XMLHttpRequest (except when the data is transfered base64 text encoded or something like this) ... so the loading of plain data is not in sync with images ... setting just the src-attrib of an image will result in a new async request that needs to be synced again with the image's onload event (at least when a sync flow is needed).

anyway ... use the delay when you want to fiddle with the concrete values that the delay should have ... it is just try and error then or just a waste of time when the value is to large only to work reliable. everybody tries to tweak performance out of a webapp and then we use delays? what logic does that follow?
Jan 19 '10 #7
brixton
35
Thanks guys, I'll try this out and let you know if I have any luck!
Jan 19 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Steve Loft | last post by:
I have a PHP script which generates responses to answers submitted by a form. The responses contain links to images. I'd like to stop users guessing the names of other images and viewing them. I...
10
by: mark | r | last post by:
anyone know a useful way of hiding image paths so images on my site cant be directly linked to? ive seen sites that use <img src="image.asp?/moo/fred/image01.jpg"> (where the path starts from...
4
by: web_design | last post by:
I put this together from some other scripts I am using on a site. I'm trying to make a better email hiding script. It isn't working. Also, it causes Internet Explorer 6 SP2 to block the script...
1
by: Ather Ali Shaikh | last post by:
Hi all, How can I edit an Image opened in the browsers and saves it with a new name. The scenario is that I received an email having some attached documents in the image format. I read that...
10
by: dave Cheseldine | last post by:
Hi I want to stop a gd generated image from being cached. It is called into index.php file as follows: <image src = "http://www.daveches.co.uk/importimage/importimage.php?x=$randomNumber"...
10
by: FX | last post by:
I wanna publish a script on my site which allows me to hide image source. i have rough idea abt it. i`ll point src to some php page like: <img src="image.php"> & in tht php wat exactly shud be...
0
by: PeterKellner | last post by:
I have an asp.net 2.0 Image control that I have surrounded with the appropriate atlas:UpdatePanel tags. The Image control referencs an aspx page whose only job is to output an Image. (it forms...
9
by: Mark Denardo | last post by:
This is related to another post I submitted, but I'll be more precise this time. I have a web page that contains an Image webcontrol that loads its image by: Image1.ImageUrl="<username>.jpg",...
11
by: Jerry J | last post by:
How can I get an asp:Image to refresh when a user uploads a different jpg. I disabled caching using this command on Page_Load(): Response.Cache.SetCacheability(HttpCacheability.No Cache); but it...
4
by: 200dogz | last post by:
Hi guys, Just a quick and simple question: i'm trying to have a loading image displayed when a submit button is pressed. Basically what I have at this stage is an asp:image with its...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.