1
jQuery(window).load(function() {
    jQuery.each(jQuery(".extraimgs"), function() {
        //this.height = 0 and this.width=0 in IE only
        if (this.height > this.width) {
            if (this.height > 263) {
                this.height = 263;
            }
            if (this.width > 354) {
                this.width = 354;
            }
        }
        else {
            this.height = 263;
            this.width = 354;
        }
    });
});

HTML Code

<asp:DataList runat="server" ID="dlImages" RepeatColumns="2" RepeatDirection="Horizontal" RepeatLayout="Table" OnItemDataBound="dlImages_ItemDataBound" Style="display: none;">
    <ItemTemplate>
        <div>
            <asp:Image runat="server" ID="imgPics" class="extraimgs" />
        </div>
    </ItemTemplate>                                 
</asp:DataList>

I have problem with this code in IE. Here I am setting the height and width of images during runtime (page load event - using jQuery) and source of images using code-behind.

This code works well in all browsers except IE8. In page load event, I get the height and width of the images, but I don't get the height and width of images in IE8. I tried a lot to get height and width of images during load event, but it does not work.

Does anyone know a solution?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
ravidev
  • 2,708
  • 6
  • 26
  • 42
  • 2
    Have you tried with `$(this).height()` and `$(this).width()`? That should work OK. – ubik May 17 '12 at 07:21
  • yes..i tried with it.but it does not work.i got 0(zero) as height and width – ravidev May 17 '12 at 07:23
  • It would be nice having a jsfiddle to play with this. You can also try doing `jQuery(".extraimgs").load(function() { /* ... */})` instead, as maybe the images haven't yet been loaded at the time that code is executed. – ubik May 17 '12 at 07:28
  • I'd have a read of [this](http://stackoverflow.com/questions/318630/get-real-image-width-and-height-with-javascript-in-safari-chrome) and also [this](http://www.jnorton.co.uk/blog/jquery-load-vs-ready) – billyonecan May 17 '12 at 07:49
  • This should work, jsfiddle link would be good to play with. – ymutlu May 17 '12 at 07:49

3 Answers3

1

OK, here's a version that should work, as per my comment above:

jQuery(function() {
    jQuery(".extraimgs").load(function() {
        var $this = $(this);
        if ($this.height > $this.width) {
            if ($this.height > 263) {
                $this.height = 263;
            }
            if ($this.width > 354) {
                $this.width = 354;
            }
        }
        else {
            $this.height = 263;
            $this.width = 354;
        }
    });
});
ubik
  • 4,440
  • 2
  • 23
  • 29
0

try this :

jQuery(document.body).load(function () {
      jQuery(".extraimgs").each(function (i) {
        if (this.height > this.width) {
            if (this.height > 263) {
                this.height = 263;
            }
            if (this.width > 354) {
                this.width = 354;
            }
        }
        else {
            this.height = 263;
            this.width = 354;
        }
      });
    });
Rohit
  • 165
  • 4
-1

what about $(document).load() or $(document).ready() instead of $(window).load()?

ndeverge
  • 21,378
  • 4
  • 56
  • 85
11684
  • 7,356
  • 12
  • 48
  • 71