Skip to main content

getting the image width from the href tag instead from img tag



i am working on a project in which i require the width of a image from href when i click on the specific image the width if the image displays in a alert .





the following is the href and image code i am using







<li>

<a href="<?php echo $pic;?>?id=<?php echo $id;?>&pic=<?php echo $picID;?>" data-title="" data-desc=" " data-rel="group2" data-bw="<?php echo $pic;?>" class="lightbox" id="plzx" >

<img src="<?php echo $pic;?>" width="160" height="160" title="Click To View"/></a>

</li>







whenever i try to get the width of the image it takes the image width from the img tag not from the href . as the img tag width is already defined "160" . is there a way possible for getting the width of the image from the <a href





i am using the following code which is nit working







$('a').click(function() {

var image = $("<img />").attr("src", $(".lightbox").attr("href"));

$(document).append(image);

alert(image.height());

alert(image.width());

image.remove();

});







the html code







<li>

<a href="uploads/kareena-kapoor-144a_$1$Ni5.St4.$kPORmHFjcfGBJjn2KHSis0.jpg?id=3&pic=101" data-title="" data-desc=" " data-rel="group2" data-bw="uploads/kareena-kapoor-144a_$1$Ni5.St4.$kPORmHFjcfGBJjn2KHSis0.jpg" class="lightbox" id="plzx" >



<img src="uploads/kareena-kapoor-144a_$1$Ni5.St4.$kPORmHFjcfGBJjn2KHSis0.jpg" width="160" height="160" title="Click To View"/></a>



</li>




Comments

  1. Based on something like:

    <a href="/path/to/big.jpg"><img src="/path/to/small.jpg"></a>


    This should do it:

    $('a').click(function() {
    $(new Image()).load(function() {
    alert(this.width); // image width
    alert(this.height); // image height
    }).attr('src', this.href);
    });


    Update: If you need to continue your program using the width/height, try:

    var width,height;
    $('a').click(function() {
    $(new Image()).load(function() {
    width = this.width;
    height = this.height,
    onLoaded();
    }).attr('src', this.href);
    });
    function onLoaded() {
    alert(width);
    alert(height);
    // continue...
    }


    You don’t need to append the temporary image to the document, but you do need to add a load listener before you extract width/height.

    Remember that the AdBlock extension can affect the outcome of the extraction in webkit, so please turn it off when debugging.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex