473,804 Members | 3,562 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.getEle mentById('my_im g');
var old_width = img.width;
img.src = url; // or img.setAttribut e('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 7079


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 onReadyStateCha nge event
of the image.
If my memory isn't wrong, img.onReadyStat eChange = function
{alert(img.read yState)};

Thiago
Aug 26 '08 #2
On Aug 25, 11:32 pm, Thiago Macedo <thiago.ch...@g mail.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 onReadyStateCha nge event
of the image.
If my memory isn't wrong, img.onReadyStat eChange = function
{alert(img.read yState)};

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...@g mail.comwrote:
>You can implement callback functions for the onReadyStateCha nge event
of the image.
If my memory isn't wrong, img.onReadyStat eChange = function
{alert(img.rea dyState)};
[...]

[...]
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 `onreadystatech ange', 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="javascrip t:document.here .src='2.jpg'>im age 2</a>
</html>
var url = "/some/request";
var img = document.getEle mentById('my_im g');
img.onload = function() { this.style.widt h = this.width+'px' ;};
// (not working with all browsers)
var old_width = img.width;
img.src = url; // or img.setAttribut e('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 <stephanemoriau x.NoAd...@wanad oo.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="javascrip t:document.here .src='2.jpg'>im age 2</a>
</html>
var url = "/some/request";
var img = document.getEle mentById('my_im g');

img.onload = function() { this.style.widt h = this.width+'px' ;};
// (not working with all browsers)
var old_width = img.width;
img.src = url; // or img.setAttribut e('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...@we b.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 onreadystatecha nge 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
1675
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 put the fullsize image tag inside a div tag. Clicking a thumbnail changes the innerHTML of the div to an image tag with the proper size and source. I'm wondering if this is the way to do it or if there's a better, more proper way?
1
3421
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 method returns immediately, and the callback function is called shortly thereafter... virtually immediately. I want to find a way to simplify my code by finding a way to simulate synchronous behavior for those functions. It's a little awkward...
9
17997
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 write "0" or "1" with a "response.write" method. The problem that I have is when I try this example with Synchronous mode. If I change the function sendNum with: xmlhttp.open("GET",url,false);
3
6156
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 to receive a return value, I was wondering is the call was performed asynchronously, rather than blocking on a call for which it would not receive a response. I was specifically thinking of the case of having a cient subscribing to the event in a...
4
3306
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 vise visa, right? So will it be a problem both server and client can initiative a sending action? TcpClient only supports synchronous operation. What does "Synchronous" mean? Means while it is reading or waiting for data to arrive from the...
1
1758
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 determined by JavaScript. So far, I can determine what list_item has been selected, but how do I determine what image button was selected? The current PHP Script & JavaScript are below. <form method="post" action="operation_request_navigation.php"...
5
1829
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 SendTimout of 2 secs. Is it normal ? Thanks in advance and regards Navin
1
3152
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 information is). I've got the product name at the top, left aligned and typically as a two lined header, and I'd like to add a small logo to the right of that, either right beside it, or in the far right corner. could anyone help me with how to set...
6
3308
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 offscreen buffer then fade this over the exisiting page. Can anyone recommend a good way to approach this? thanks
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9582
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10580
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10082
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9157
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6854
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3821
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.