Solved

Problem in Script which can resize the picture at load time

Posted on 2006-07-02
3
170 Views
Last Modified: 2010-04-06
hello Experts

This is my code

<script language=vbscript>
function ChangeHeight(oh,ow,nh,imgnm)
   nw= (nh*ow)/oh
  alert(nw)
end function
</script>


<td><P align=center><A  href="http://www.artageindia.com/bigpic.asp?id1=36">  <IMG id="img1"
            src="products1_files/BC-011.jpg" border=2 onload="ChangeHeight(this.height,this.width,77,this.id)" > </p></td>
<td><P align=center><A  href="http://www.artageindia.com/bigpic.asp?id1=37">  <IMG id="img2"
            src="products1_files/BC-012.jpg" border=2 onload="ChangeHeight(this.height,this.width,77,this.id)" > </p></td>
<td><P align=center><A  href="http://www.artageindia.com/bigpic.asp?id1=38">  <IMG id="img3"
            src="products1_files/BC-013.jpg" border=2 onload="ChangeHeight(this.height,this.width,77,this.id)" > </p></td>

now i want to change the width and height of the below images (img1,img2,img3) so that all the pictures height remain same and width can be changed accordingly using function. Now using function how to change the size of pictures run time.

thanks

vikas
0
Comment
Question by:vikas_maderna
  • 2
3 Comments
 
LVL 26

Expert Comment

by:DireOrbAnt
Comment Utility
Some notes first. I would not use vbscript as javascript has a  MUCH better support base.
Also, I used Math.round so you don't get non int width.

Here it is:
------------------------------
<HTML>
<HEAD>
<script language="javascript">
function ChangeHeight(ImgId, nh) {
  var ImgObj = document.getElementById(ImgId);
  ImgObj.width = Math.round((nh*ImgObj.width)/ImgObj.height);
}

function FixImages() {
  ChangeHeight('img1', 77);
  ChangeHeight('img2', 77);
  ChangeHeight('img3', 77);      
}
</script>
</HEAD>
<BODY onload="FixImages()">
<table><tr>
<td><P align=center><A  href="http://www.artageindia.com/bigpic.asp?id1=36">  <IMG id="img1"
            src="products1_files/BC-011.jpg" border=2> </p></td>
<td><P align=center><A  href="http://www.artageindia.com/bigpic.asp?id1=37">  <IMG id="img2"
            src="products1_files/BC-012.jpg" border=2> </p></td>
<td><P align=center><A  href="http://www.artageindia.com/bigpic.asp?id1=38">  <IMG id="img3"
            src="products1_files/BC-013.jpg" border=2> </p></td>
</tr></table>
</BODY>
</HTML>
------------------------------
0
 

Author Comment

by:vikas_maderna
Comment Utility
hello ,

In this function

function FixImages() {
  ChangeHeight('img1', 77);
  ChangeHeight('img2', 77);
  ChangeHeight('img3', 77);    
}

You have made the img1 and img2, .... name fixed while for me this is variable thing, as it is asp page and pictures are comming from database and i dont know their numbers. Then how can i do this.

thanks

vikas
0
 
LVL 26

Accepted Solution

by:
DireOrbAnt earned 500 total points
Comment Utility
How about this:
-------------------------------
<HTML>
<HEAD>
<script language="javascript">
function ChangeHeight(ImgObj, nh) {
  ImgObj.width = Math.round((nh*ImgObj.width)/ImgObj.height);
}

function FixImages() {
  var AllImg = document.getElementsByTagName('IMG');
  for (var i=0; i<AllImg.length; i++) {
        if (AllImg[i].className == 'MyClass') ChangeHeight(AllImg[i], 77);
      }
}
</script>
</HEAD>
<BODY onload="FixImages()">
<table><tr>
<td><P align=center><A  href="http://www.artageindia.com/bigpic.asp?id1=36">  <IMG id="img1" src="products1_files/BC-011.jpg" border=2> </p></td>
<td><P align=center><A  href="http://www.artageindia.com/bigpic.asp?id1=37">  <IMG id="img2" class="MyClass" src="products1_files/BC-012.jpg" border=2> </p></td>
<td><P align=center><A  href="http://www.artageindia.com/bigpic.asp?id1=38">  <IMG id="img3" src="products1_files/BC-013.jpg" border=2> </p></td>
</tr></table>
</BODY>
</HTML>
-------------------------------

Note that I changed both functions and made the second image class be MyClass so it changes that one.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now