473,399 Members | 3,401 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,399 software developers and data experts.

Changing image src, is it a synchronous operation?

I have a simple task:
I need to change image src on the fly and then get its new width and
height;

var url = "/some/request";
var img = document.getElementById('my_img');
var old_width = img.width;
img.src = url; // or img.setAttribute('src', url);
//Now in browser I got a new picture
var new_width = img.width;
alert(new_width-old_width);

For some reason I get "0" in both FF and IE. Does it mean that
changing image source is an asynchronous procedure? If it's so can I
make it synchronous or get a callback on completion?
Any ideas how to get new width in the same function?

Thank you!
Andrew
Aug 25 '08 #1
7 7056


Trastabuga wrote:
For some reason I get "0" in both FF and IE. Does it mean that
changing image source is an asynchronous procedure? If it's so can I
make it synchronous or get a callback on completion?
Any ideas how to get new width in the same function?
Hi,

Yes, its an asynchronous proc.
You can implement callback functions for the onReadyStateChange event
of the image.
If my memory isn't wrong, img.onReadyStateChange = function
{alert(img.readyState)};

Thiago
Aug 26 '08 #2
On Aug 25, 11:32 pm, Thiago Macedo <thiago.ch...@gmail.comwrote:
Trastabuga wrote:
For some reason I get "0" in both FF and IE. Does it mean that
changing image source is an asynchronous procedure? If it's so can I
make it synchronous or get a callback on completion?
Any ideas how to get new width in the same function?

Hi,

Yes, its an asynchronous proc.
You can implement callback functions for the onReadyStateChange event
of the image.
If my memory isn't wrong, img.onReadyStateChange = function
{alert(img.readyState)};

Thiago
Thank you, Thiago.
For some reason it only works in IE for me. FF just ignores it.
Aug 26 '08 #3
Trastabuga wrote:
On Aug 25, 11:32 pm, Thiago Macedo <thiago.ch...@gmail.comwrote:
>You can implement callback functions for the onReadyStateChange event
of the image.
If my memory isn't wrong, img.onReadyStateChange = function
{alert(img.readyState)};
[...]

[...]
For some reason it only works in IE for me. FF just ignores it.
It is rather surprising it works at all; are you sure about that? For it
should be `onreadystatechange', ECMAScript implementations are
case-sensitive. However, it is not surprising that Firefox would ignore
that; this property is MSHTML-proprietary, it has been implemented by Gecko
only with XHR as it is also part of MSXML from which this particular
interface originates.

Since JavaScript 1.1, Netscape 2.0, Image objects have an `onload' event
handler property that can be used. Unsurprisingly, it is also supported by
the MSHTML DOM. So you should use

// add feature tests here
var img = new Image();

img.onload = function() {
// ...
};

img.src = "...";

The very same question has been asked and answered (by me) two days ago.
Please search before you post. After all, you are using Google Groups for
posting, so doing a little research before bothering people with old
problems should not be too hard.

<http://jibbering.com/faq/>
PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Aug 26 '08 #4
Thomas 'PointedEars' Lahn wrote:
Since JavaScript 1.1, Netscape 2.0, Image objects have an `onload' event
^^^^^^^^^^^^
handler property that can be used. [...]
Should read "Netscape 3.0". (JavaScript was introduced with Netscape 2.0.)
The very same question has been asked and answered (by me) two days ago.
Please search before you post. After all, you are using Google Groups for
posting, so doing a little research before bothering people with old
^^^^^^^^^^^^^^^^
problems should not be too hard.
Should read "... instead of bothering ...".
PointedEars
Aug 26 '08 #5
SAM
Trastabuga a écrit :
I have a simple task:
I need to change image src on the fly and then get its new width and
height;
Why to use Ajax for it (that browsers can make for the last century)

It is absolutly simple to do
(just notifying visitor he has to wait the new image)
don't give height nor width nor sizes styles to your image

<html>
<img name="here" src="1.jpg">
<a href="javascript:document.here.src='2.jpg'>image 2</a>
</html>
var url = "/some/request";
var img = document.getElementById('my_img');
img.onload = function() { this.style.width = this.width+'px';};
// (not working with all browsers)
var old_width = img.width;
img.src = url; // or img.setAttribute('src', url);
//Now in browser I got a new picture
no, you have to wait it's complete loading !
var new_width = img.width;
alert(new_width-old_width);
--
sm
Aug 26 '08 #6
On Aug 26, 7:04 am, SAM <stephanemoriaux.NoAd...@wanadoo.fr.invalid>
wrote:
Trastabuga a écrit :
I have a simple task:
I need to change image src on the fly and then get its new width and
height;

Why to use Ajax for it (that browsers can make for the last century)

It is absolutly simple to do
(just notifying visitor he has to wait the new image)
don't give height nor width nor sizes styles to your image

<html>
<img name="here" src="1.jpg">
<a href="javascript:document.here.src='2.jpg'>image 2</a>
</html>
var url = "/some/request";
var img = document.getElementById('my_img');

img.onload = function() { this.style.width = this.width+'px';};
// (not working with all browsers)
var old_width = img.width;
img.src = url; // or img.setAttribute('src', url);
//Now in browser I got a new picture

no, you have to wait it's complete loading !
var new_width = img.width;
alert(new_width-old_width);

--
sm
Thanks, onload works!
Aug 26 '08 #7
On Aug 26, 6:41 am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Thomas 'PointedEars' Lahn wrote:
Since JavaScript 1.1, Netscape 2.0, Image objects have an `onload' event

^^^^^^^^^^^^
handler property that can be used. [...]

Should read "Netscape 3.0". (JavaScript was introduced with Netscape 2.0.)
The very same question has been asked and answered (by me) two days ago.
Please search before you post. After all, you are using Google Groups for
posting, so doing a little research before bothering people with old

^^^^^^^^^^^^^^^^
problems should not be too hard.

Should read "... instead of bothering ...".

PointedEars
Thomas, you are right onreadystatechange only works with IE when in
lower case. I really searched the Net but couldn't find the answer,
probably used the wrong wording. Sorry to have bothered you.
Aug 26 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Lazarus Plath | last post by:
I've made a gallery that has thumbnails along the top and clicking them changes the source for the full size image below, but it seems I can't change the image size. A workaround I made up is to...
1
by: Chris | last post by:
Hi. I have a ibrary I'm trying to use via javascript within IE. This library uses an asynchronous model where I call into a function and pass it a callback function as one of its arguments. My...
9
by: David | last post by:
Hello I'm testing the XMLHttpRequest object in Firefox and IE. The code below works well in IE and Firefox. It shows "1" when the string is a number and "0" when not. The page aspxTest.aspx only...
3
by: Al | last post by:
Hi, I believe that when an event is Raised is C#, it is performed synchronously, as a value is returned by the event handler, even if that value is NULL. However, as VB does not have the ability...
4
by: Ryan Liu | last post by:
TcpClient has a method called GetworkStream GetStream(); So in other words, there is only one stream associate with it for input and output, right? So while it is receiving, it can not send, and...
1
by: automation | last post by:
I have a web-page consisting of a listbox & image buttons. I want to post to the appropriate PHP script based on which list_item and image button combination is selected, where the logic is...
5
by: Navin Mishra | last post by:
Hi, In load test of our .NET 2.0 socket application on Win2003 server, we are seeing sometimes WSEWOULDBLOCK error when sending data to clients. We are using synchronoous scokets with...
1
by: rsteph | last post by:
I've got some product information pages, with images and text, all setup within a table. I'm trying to add a small image in the upper right hand corner of the content div (where all the important...
6
by: Martin Slater | last post by:
Hi all, I'm using a webbrowser control within an application for the UI and want to hide the flicker and redraw when changing pages. Ideally I want to render the new page to a seperate...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.