2

I found a javascript code that get and set the dimension of image .
By reference this link , I write the code in my asp.net web site likes this ,

<img runat="server" id="MyImage" onload="if( this.width >  
this.height ){ this.width = 400; this.height = 600} else {this.width = 600;   
this.height=400}" />

What I want to know is how to write this javascript code in this <script> tag

 <script type="text/javascript">
   function setDimension() {

   }
</script>
1
  • If my javascript code is wrong , Kindly show me the correct code :), Thanks ! Commented May 11, 2013 at 5:38

1 Answer 1

1

Try this

Javascript:

<script type="text/javascript">
   function setDimension(me) {
  var th= document.getElementById(me);
   if( th.width >  
th.height ){ th.width = 400; th.height = 600} else {th.width = 600;   
th.height=400}
   }
</script>

ASPX Code:

<img runat="server" id="MyImage"  />

In code behind, as onload is also a server event, you need to add custom attribute to server image control

 MyImage.Attributes["onload"] = "setDimension(this.id)";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.