Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: what's with this image?

  1. #1
    Join Date
    Nov 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation what's with this image?

    Code:
    <html>
    <head>
    <title>Example</title>
    <script type="text/javascript">
    function size()
    {
    var a = document.getElementById('img').style.width;
    var b = document.getElementById('img').style.height;
    if(a != 150){
    	document.getElementById('img').style.width = 150;
    	document.getElementById('img').style.height = 200;
    }else{
    	document.getElementById('img').style.width = 300;
    	document.getElementById('img').style.height = 500;
    	}
    	return false;
    	
    }
    </script>
    </head>
    <body>
    
    <img id="img" style="width:300px; height:500px; cursor:pointer" onClick="size()"src="zoro15.jpg" alt=""
     />
    </body>
    </html>



    why isn't it resizing back to normal?
    Last edited by jscheuer1; 11-10-2007 at 07:34 AM. Reason: add code tags

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    a will never = 150 in a browser that complies to standards. The style width property is a string that includes the type of units being used, not a raw number as you are trying to make it be. If you try to assign it as just a number, many browsers will refuse to, others may append 'px' to it. Either way, it will never equal 150.

    There could be other problems.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Nov 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    is there a better way around this?

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by khaled450 View Post
    is there a better way around this?
    Well, the main thing is that you need to take into account the facts I mentioned. And, yes there is a better way, any way that worked would be better. There are, in fact 'many ways to skin a cat.' Here's one:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>Example</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    function size(el){
    var s = el.style, w = 'width', h = 'height';
    	if(s[w] != '150px'){
    		s[w] = '150px';
    		s[h] = '200px';
    	}
    	else {
    		s[w] = '300px';
    		s[h] = '500px';
    	}
    };
    </script>
    </head>
    <body>
    <div>
    <img style="width:300px; height:500px; cursor:pointer;"
    onclick="size(this)" src="zoro15.jpg" alt="">
    </div>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Dec 2006
    Posts
    47
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default

    I've always had trouble with the 'px' deal. Here's what I whipped up from the original code...

    PHP Code:
    <html>
    <
    head>
    <
    title>Example</title>
    <
    script type="text/javascript">
    function 
    size()
    {
    var 
    document.getElementById('img').offsetWidth;
    var 
    document.getElementById('img').offsetHeight;
    if(
    != 150){
        
    document.getElementById('img').style.width '150px';
        
    document.getElementById('img').style.height '200px';
    }else{
        
    document.getElementById('img').style.width '300px';
        
    document.getElementById('img').style.height '500px';
        }
        return 
    false;
        
    }
    </script>
    </head>
    <body>

    <img id="img" style="width:300px; height:500px; cursor:pointer" onClick="size()"src="zoro15.jpg" alt=""
     />
    </body>
    </html> 

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by jscheuer1 View Post
    There are, in fact 'many ways to skin a cat.' Here's one . . .
    Quote Originally Posted by Kenny View Post
    I've always had trouble with the 'px' deal. Here's what I whipped up from the original code . . .
    Poor cat. Yep, that looks like another way. However, even with your trouble with the 'px' deal, I see that you use (for example):

    Quote Originally Posted by Kenny View Post
    Code:
     . . . g').style.width = '150px';
    which acknowledges:

    Quote Originally Posted by me
    The style width property is a string that includes the type of units being used, not a raw number . . .
    and uses the 'px' deal. There's no real way to totally avoid the facts of the matter.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
            var s = el.style, w = 'width', h = 'height';
            if(s[w] != '150px'){
                    s[w] = '150px';
                    s[h] = '200px';
            }
    I've noticed this pattern in your code of late. Why do you do it?

    Also, you've started indenting now?
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Twey View Post
    I've noticed this pattern in your code of late. Why do you do it?
    I am assuming you mean condensing via variable assignment. Isn't that the way efficient machine code works? In any case, sometimes I just get tired typing, and/or the ideas come too fast.

    Quote Originally Posted by Twey View Post
    Also, you've started indenting now?
    You are hard to please. If I condense my code 'you complain.' If I expand it 'you complain.'

    In this particular case I just thought I would give the OP a break so that they could read it more easily.

    I like to think of myself as an aspiring code virtuoso. I can write it . . . however, and still know what it means. I stress the word 'aspiring', but I'm making progress.

    Sometimes I'm in a hurry. Some other times I'm stopping to smell the roses. But most times I try to make the code as clear and as efficient as the particular situation calls for.

    It has been my experience that folks will often ask questions if they don't initially get the' how and why', and want to.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    You are hard to please. If I condense my code 'you complain.' If I expand it 'you complain.'
    Oh, on the contrary, I'm quite pleased about it, I was just surprised since you've previously said you hated indentation in any form and found that you had to try harder to read even properly-indented code.
    I am assuming you mean condensing via variable assignment. Isn't that the way efficient machine code works? In any case, sometimes I just get tired typing, and/or the ideas come too fast.
    Ah, the slowdown comes from the lookup: if we have a.b.c.d and we access it multiple times, it's more efficient to store e = a.b.c then access it as e.d. For a single property name, there is no slowdown, and I'd suspect that assigning it to a variable and then doing a string lookup would actually be slower, albeit only marginally.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    There is a reason why I put 'you complain' in quotes. I wasn't saying that you really were upset or even expressing that you were. In any case, the indenting or lack thereof is never meant to be for or against you, or anyone.

    I'm not all about top code efficiency, but I do like to adopt, as I go along, techniques for it*. I also reserve the right to write as I see fit, as it were, 'as it comes off my pen', and as I see fit to edit it.



    *to wit - I'm going to be using a lot more of:

    Code:
    for (var i=some.length-1 ; i>-1; --i)
    than I have heretofore.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •