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

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.