简体   繁体   中英

Getting image width using javascript have problem?

This is the code i got from net to get the image width. It works great.

<script type="text/javascript">
function CreateDelegate(contextObject, delegateMethod)
{   
        return function()
            {
                return delegateMethod.apply(contextObject, arguments);
            }
}


function imgTesting_onload()
{
    image_width=this.width;
    alert(image_width);//First Alert
}
</script>
var imgTesting = new Image();
imgTesting.onload = CreateDelegate(imgTesting, imgTesting_onload);
imgTesting.src = "${str_applicationPath}/common/images/reports/"+report+".png"
//Second Alert//

But my need is to access the image_width variable at the //Second Alert// place in the above code.I am not getting the width value there.Is anything to be done to get value there?

Please Help Me......... Thanks in Advance

That's not possible. At that point the width is not known. That is the whole point of the onload event in the first place. You have to execute the code you want at "Second alert" in imgTesting_onload instead.

you can use imgTesting.width to get image width.

For example:

var imgTesting = new Image();

imgTesting.src => "${str_applicationPath}/common/images/reports/"+report+".png"

//Second Alert//

alert(imgTesting.width);

Below given code is not required

function CreateDelegate(contextObject, delegateMethod)
{   
        return function()
            {
                return delegateMethod.apply(contextObject, arguments);
            }
}
function imgTesting_onload()
{
    image_width=this.width;
    alert(image_width);//First Alert
}
imgTesting.onload =CreateDelegate(imgTesting, imgTesting_onload);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM