Get Image Size in FireFox

Discussion in 'JavaScript' started by monkeyvu, Feb 22, 2008.

  1. #1
    Hi all,

    I'm getting image size in FF but I couldn't.

    image = new Image();
    image.src = 'location';

    then image.width and image.height just return 0

    It's working with IE but FF. I have already searched but can't find any solution. Please help me, thanks very much :)
     
    monkeyvu, Feb 22, 2008 IP
  2. greboguru

    greboguru Member

    Messages:
    78
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #2
    Hi,

    I have to say it works OK for me. Does the image your loading display OK? i.e. is it actually loading or is your location incorrect?
     
    greboguru, Feb 22, 2008 IP
  3. monkeyvu

    monkeyvu Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    s = document.getElementById("heroImage").value.replace(/^\s*|\s*$/g,"");
    		s = s.substring(s.lastIndexOf(".") + 1);		
    		
    		if (s.length > 0)
    		{
    			if ((s.toLowerCase() != "jpg") && (s.toLowerCase() != "jpeg") && (s.toLowerCase() != "gif")) {
    				alertS = alertS + "Hero image must be jpg or gif\n";
    			}
    			image = new Image();
    			image.src = document.getElementById("heroImage").value.replace(/^\s*|\s*$/g,"");
    			
    			if (image.width != 192 || image.height != 215) {
    				alertS = alertS + "Hero image size must be 192 x 215\n";
    			}
    			if (document.getElementById("captionHero").value.replace(/^\s*|\s*$/g,"").length <= 0) {
    				alertS = alertS + "Please enter a hero image caption\n";
    			}
    		}
    Code (markup):
    Here is the snippet of code. It's working with IE.
     
    monkeyvu, Feb 22, 2008 IP
  4. greboguru

    greboguru Member

    Messages:
    78
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #4
    OK ...

    I built a sample page using your code and what I've found is that I can get a image width and height in IE but no height or width in FF.

    If I try and load an image in IE that doesn't exist I still get a width for it of 28. FF shows a height of 0. If I supply a valid image file name then I get a height and width in both IE and FF. It seems that if IE can't find the image it still has a height?

    Is this what your seeing?
     
    greboguru, Feb 22, 2008 IP