dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Image Height in Firefox

  1. #1
    Join Date
    Nov 2003
    Posts
    6

    Image Height in Firefox

    Hello,

    I'm trying to get the image dimensions through javascript. Working great in IE but Firefox it is always set to 0.

    Here's what I have:

    function add_image(src)
    {
    var image = new Image();
    image.src = src;
    var ratio = get_aspect_ratio(image.width, image.height);
    image.height *= ratio;
    image.width *= ratio;
    // Blah Blah
    }

    In firefox the image.width and image.height are 0. Is there a way to get those dimensions in firefox?

    Cheers,
    lee20

  2. #2
    Nigel McFarlane is offline Former moderator, account closed
    Join Date
    Nov 2003
    Location
    I live on the 'net
    Posts
    229
    You can't be sure that the image has loaded in this code. It's just an accident of your system that IE works and Firefox doesn't. For someone else, the reverse could be true.

    After you set the .src property, it might take some time before the image is in the browser. You need to set an .onload handler on the Image object so that you can check when it's loaded.
    Nigel McFarlane, Author: "Firefox Hacks", "Rapid Application Development with Mozilla"

  3. #3
    Join Date
    Apr 2007
    Posts
    1

    You can do it ;)

    Hello, if you create an image with new Image() you can't get the with & the height. With Ie you can get the Height & the With of a local image but with Firefox it's doesn't work.

    So here the solution :

    Just select a local file with the input file and after click on "Toto" you reveice the height & the width of your image.

    The piece of code :

    <html>
    <body>
    <form name="frm">
    <input type="file" name="myfile" id="myfile">
    </form>
    <br>
    <a href="javascript:void(0)" onclick="toto();">toto</a>
    <br>
    <span id="fun">xxx</span>

    <script>
    function toto()
    {
    alert( document.getElementById("myfile").value )

    s="<table border=1><tr><td><img src='"+document.getElementById("myfile").value+"' id='myimage'></td></tr></table>";
    document.getElementById("fun").innerHTML=s;

    alert( document.getElementById("myimage").width )
    alert( document.getElementById("myimage").Height )
    }
    </script>
    </body>
    </html>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center