image size problem

Discussion in 'Javascript' started by xdevel1999, Dec 5, 2008.

  1. xdevel1999

    xdevel1999 Guest

    Hi,

    I have the following problem:

    the FIRST time I load a page with Firefox 3, always with Safari/Chrome
    and never with Opera/IE7

    the image.width and heigth are not filled but they have a 0 pixel size

    Ex.

    i = new Image()
    i.src = "image.png"

    // here the problem
    i.width
    i.height

    are 0

    May be is a loading problem? I try to get that values before the
    images are fully loaded?

    How can I solve that?

    Thanks
     
    xdevel1999, Dec 5, 2008
    #1
    1. Advertising

  2. xdevel1999

    Erwin Moller Guest

    xdevel1999 schreef:
    > Hi,
    >
    > I have the following problem:
    >
    > the FIRST time I load a page with Firefox 3, always with Safari/Chrome
    > and never with Opera/IE7
    >
    > the image.width and heigth are not filled but they have a 0 pixel size
    >
    > Ex.
    >
    > i = new Image()
    > i.src = "image.png"
    >
    > // here the problem
    > i.width
    > i.height
    >
    > are 0
    >
    > May be is a loading problem? I try to get that values before the
    > images are fully loaded?


    Makes sense yes.
    I am not sure, but maybe getting the height and width wait with their
    response untill the image is loaded?

    Anyway: You could test if the image is loaded before trying to read its
    dimensions.
    You can use the complete property for this (returns boolean)

    Propably it makes more sense to attach the 'onload' eventhandler to the
    image.
    i.onload = yourfunctionhere

    Your function 'yourfunctionhere' will be called when the image is 100% in.

    Regards,
    Erwin Moller

    >
    > How can I solve that?
    >
    > Thanks
    >
    >



    --
    "There are two ways of constructing a software design: One way is to
    make it so simple that there are obviously no deficiencies, and the
    other way is to make it so complicated that there are no obvious
    deficiencies. The first method is far more difficult."
    -- C.A.R. Hoare
     
    Erwin Moller, Dec 5, 2008
    #2
    1. Advertising

  3. xdevel1999

    xdevel1999 Guest

    Sorry for the late...

    > You can use the complete property for this (returns boolean)
    > Propably it makes more sense to attach the 'onload' eventhandler to the
    > image.
    > i.onload = yourfunctionhere


    yes both are good but the problem is that if I do:

    function()
    {
    img.onload = fun() { dosomething() }

    a = img.width;
    b =img.height;
    }

    above the code of getting img properties fails because it is executed
    before of the onload image function that it does not completed yet
    I don't want put a timer bacause the images can be a lot

    Thanks
     
    xdevel1999, Dec 10, 2008
    #3
  4. xdevel1999

    David Mark Guest

    On Dec 10, 2:53 am, xdevel1999 <> wrote:
    > Sorry for the late...
    >
    > > You can use the complete property for this (returns boolean)
    > > Propably it makes more sense to attach the 'onload' eventhandler to the
    > > image.
    > > i.onload = yourfunctionhere

    >
    > yes both are good but the problem is that if I do:
    >
    > function()
    > {
    >    img.onload = fun() { dosomething() }


    Fix the typo and put the following two lines in this function.

    >
    >    a = img.width;
    >    b =img.height;
    >
    > }
    >


    [snip]
     
    David Mark, Dec 10, 2008
    #4
  5. xdevel1999

    xdevel1999 Guest

    On 10 Dic, 09:06, David Mark <> wrote:
    > On Dec 10, 2:53 am, xdevel1999 <> wrote:
    >
    > > Sorry for the late...

    >
    > > > You can use the complete property for this (returns boolean)
    > > > Propably it makes more sense to attach the 'onload' eventhandler to the
    > > > image.
    > > > i.onload = yourfunctionhere

    >
    > > yes both are good but the problem is that if I do:

    >
    > > function()
    > > {
    > >    img.onload = fun() { dosomething() }

    >
    > Fix the typo and put the following two lines in this function.
    >
    >
    >
    > >    a = img.width;
    > >    b =img.height;

    >
    > > }

    >
    > [snip]


    Do Fix the typo? maybe do you mean to the fun() ? oh I wrote them for
    abbr. sure it must be function()
    or what else?
     
    xdevel1999, Dec 10, 2008
    #5
  6. xdevel1999

    Erwin Moller Guest

    xdevel1999 schreef:
    > Sorry for the late...
    >
    >> You can use the complete property for this (returns boolean)
    >> Propably it makes more sense to attach the 'onload' eventhandler to the
    >> image.
    >> i.onload = yourfunctionhere

    >
    > yes both are good but the problem is that if I do:
    >
    > function()
    > {
    > img.onload = fun() { dosomething() }
    >
    > a = img.width;
    > b =img.height;
    > }
    >
    > above the code of getting img properties fails because it is executed
    > before of the onload image function that it does not completed yet
    > I don't want put a timer bacause the images can be a lot


    You approach it wrong.
    Don't ask for the weight and height UNTILL your function coupled to
    'onload' is called.

    You are asking JavaScript to give you the width and height of images
    that have not arrived yet.

    >
    > Thanks
    >
    >
    >
    >
    >



    --
    "There are two ways of constructing a software design: One way is to
    make it so simple that there are obviously no deficiencies, and the
    other way is to make it so complicated that there are no obvious
    deficiencies. The first method is far more difficult."
    -- C.A.R. Hoare
     
    Erwin Moller, Dec 11, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UGFyYWtp?=

    Image size problem. Image Compression

    =?Utf-8?B?UGFyYWtp?=, Jun 24, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    500
    Kevin Spencer
    Jun 24, 2005
  2. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,663
    Michael Jung
    May 25, 2008
  3.  Tom
    Replies:
    3
    Views:
    141
    Robert Klemme
    Nov 11, 2009
  4. Bob Richardson
    Replies:
    3
    Views:
    247
    Bob Richardson
    Mar 8, 2005
  5. bluedolphin
    Replies:
    1
    Views:
    167
Loading...

Share This Page