473,405 Members | 2,415 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Changing image width using JavaScript

Hi,

I have the following coding on a web page. It causes two pictures (pic1.jpg
and pic2.jpg)
to show, one above the other and then when one clicks on the top picture is
squeezes to
the left (as its width is reduced) to show the bottom picture. Then when the
bottom
picture is clicked the top picture expands to the right to cover the bottom
picture again.
Much of the coding is simply to show the pictures a suitable size for the
user's screen
size.
<script language="JavaScript">
<!--
var scnwid=screen.width;
var scnhei=screen.height;
var ii=0;
var maxii=20;
var widthf=0;
var widthw=0;
var winc=0;
var topd=10;
var leftd=0;
var heightf=350;
var msgtop=0;
function Init() {
// set widths and heights of pictures and divs to match screen size
widthf=0.875*scnwid;
heightf=.6*scnhei;
leftd=(scnwid-widthf)/2;
widthw=widthf;
document.pic1.width = widthf;
document.pic1.height = heightf;
var ref=document.getElementById("div1");
ref.style.top=topd;
ref.style.left=leftd;
ref.style.width=widthf;
ref.style.height=heightf;
document.pic2.width = widthf;
document.pic2.height = heightf;
var ref=document.getElementById("div2");
ref.style.top=topd;
ref.style.left=leftd;
ref.style.width=widthf;
ref.style.height=heightf;
msgtop=heightf+topd;
var ref3=document.getElementById("div3");
ref3.style.top=msgtop;
ref3.style.left=leftd;
}

function ChgSize(updown) {
winc=widthf/maxii;
winc=updown*winc;
ii=0;
DoPics();
}

function DoPics() {
widthw=widthw-winc;
document.pic1.width = widthw;
ii=ii+1;
if (ii<maxii) {
setTimeout("DoPics()",30);
}
}

-->
</script>
</head>

<body onLoad="Init();return true">

<div id=div1
style="position:absolute;left:10;top:10;width:700; height:350;z-index:1;">
<a href="javascript:void(0)" onClick="ChgSize(1); return true"><img
name="pic1" border=0 src="pic1.jpg" width=700 height=400></a>
</div>

<div id=div2
style="position:absolute;left:10;top:10;width:700; height:350;z-index:0;">
<a href="javascript:void(0)" onClick="ChgSize(-1); return true"><img
name="pic2" border=0 src="pic2.JPG" width=700 height=400></a>
</div>

<p>
<center>
<div id=div3 style="position:absolute;left:10;top:10;z-index:0;">
Click on the picture to see the other picture.
</div>
</center>

The coding and web page work fine when I display it using the web page
editor I wrote in
Microsoft Visual Basic 6 but not when I view the page using Internet
Explorer. When I do
the latter there are often, but not always, white flashes that appear as the
one picture
width is reduced or expanded. It acts as if the picture with the new width
is not fully
displayed before it is time to reduce it by the next increment.

I am surprised by this since I thought that the browser window in my web
page editor used
the same coding as Internet Explorer.

I have tried to preload the pictures but this did not make any difference.

Can someone help me with this problem? Is there a better way to do the same
thing?

Thank you.
Doug vV
Jan 6 '06 #1
4 3439
"Doug van Vianen" <co*****@shaw.ca> wrote in
news:NqBvf.258456$ki.258172@pd7tw2no:
Can someone help me with this problem? Is there a better way to do the
same thing?


I am *guessing* that under your IE "Internet Options" / Temporary
Internet Files / Settings, you have your 'check for newer versions of
stored pages' set to "Every Visit To The Page'.

This tends to cause an image to flash/disappear each time it is changed
via javascript - nothing is cached so it must reload, THEN go through
your transformation. You can either change your settings, or pretty
much live with it. There *are* some workarounds if you look hard enough
on google... most involve changing some settings in .htaccess

You can confirm whether or not this is your problem by looking at the
page in Firefox or changing your settings in IE.


Jan 6 '06 #2

Doug van Vianen wrote:
<script language="JavaScript">
The language attribute is deprecated, use the type attribute as
follows:

type = "text/javascript"
<!--
HTML comments are unnecessary.
document.pic1.width = widthf;
document.pic1.height = heightf;
document.pic2.width = widthf;
document.pic2.height = heightf;
To affect the width and the height, you also need to access the style
property:

document.picX.style.width = widthf;
document.picX.style.height = heightf;
<body onLoad="Init();return true">
There is need to return a boolean value. Typically it's also a better
idea to programmatically assign a function reference through javascript
rather than through the body onload attribute.

window.onload = Init;
<div id=div1
It's good practice to place quotes around the values.

<div id = "div1" ...
<a href="javascript:void(0)" onClick="ChgSize(1); return true">
The use of javascript pseudo protocol is highly frowned upon.
<img name="pic1" border=0 src="pic1.jpg" width=700 height=400></a>


Why not try placing the onclick on the img instead?

<img src = "pic1.jpg" onclick = "ChgSize(1)">

Jan 7 '06 #3
Hi,

Thank you for your reply.

I checked the settings for my IE and they were okay. In any case, I would
like the coding to work for anyone's browser without them having to change
settings. (Great idea but a little unrealistic!)

I changed the coding to use a series of DIVs, each with the picture to be
shrunk or expanded at a different level of shrinking or expansion. To do the
shrinking or expanding I just change which of the DIVs is visible. While
this is not very sophistocated it seems to work. I have not yet, however,
checked this on other computers.

Thank you again.

Doug vV

"Good Man" <he***@letsgo.com> wrote in message
news:Xn************************@216.196.97.131...
"Doug van Vianen" <co*****@shaw.ca> wrote in
news:NqBvf.258456$ki.258172@pd7tw2no:
Can someone help me with this problem? Is there a better way to do the
same thing?


I am *guessing* that under your IE "Internet Options" / Temporary
Internet Files / Settings, you have your 'check for newer versions of
stored pages' set to "Every Visit To The Page'.

This tends to cause an image to flash/disappear each time it is changed
via javascript - nothing is cached so it must reload, THEN go through
your transformation. You can either change your settings, or pretty
much live with it. There *are* some workarounds if you look hard enough
on google... most involve changing some settings in .htaccess

You can confirm whether or not this is your problem by looking at the
page in Firefox or changing your settings in IE.

Jan 8 '06 #4
Hi,

Thank you for your reply. I will update my coding and perhaps also update
my library of books on JavaScript.

Doug vV

"web.dev" <we********@gmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...

Doug van Vianen wrote:
<script language="JavaScript">


The language attribute is deprecated, use the type attribute as
follows:

type = "text/javascript"
<!--


HTML comments are unnecessary.
document.pic1.width = widthf;
document.pic1.height = heightf;
document.pic2.width = widthf;
document.pic2.height = heightf;


To affect the width and the height, you also need to access the style
property:

document.picX.style.width = widthf;
document.picX.style.height = heightf;
<body onLoad="Init();return true">


There is need to return a boolean value. Typically it's also a better
idea to programmatically assign a function reference through javascript
rather than through the body onload attribute.

window.onload = Init;
<div id=div1


It's good practice to place quotes around the values.

<div id = "div1" ...
<a href="javascript:void(0)" onClick="ChgSize(1); return true">


The use of javascript pseudo protocol is highly frowned upon.
<img name="pic1" border=0 src="pic1.jpg" width=700 height=400></a>


Why not try placing the onclick on the img instead?

<img src = "pic1.jpg" onclick = "ChgSize(1)">

Jan 8 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Kenny | last post by:
I have been trying to write a script that will increase the size of and image when you mouse over it, and decrease it to original size, when you mouse out. After a couple of attempts, this is what...
2
by: Ian F | last post by:
I have some javascript which allows a header, iframe and picture to be changed when the user clicks a next/previous button. In Opera, if you click next enough times to loop back to the start, or...
5
by: Csaba Gabor | last post by:
Is there any way to determine the pixel height and width of an original image? Specifically, If I have <IMG id=myImg src="pic.jpg" height=200 width=300> can I figure out what the original size...
6
by: abdullah1983 | last post by:
Hi Guys, I need some clarification regarding the problem with safari browser. Please find my code below. I'm setting the image src, mouseover and mouseout using javascript. The mouseover and...
3
by: bulldog8 | last post by:
I am having problems with changing a frames source file for Mozilla (1.5.0.4). The followig code works for IE: <script type="text/javascript"> var imgP = new Image(); function...
6
by: Seth Illgard | last post by:
Hi, Im writting a custom CMS and everything looks great, except when I see the results in IE. What im trying to do is: *Have an image in a layer (or relative positioned, or just margined). The...
2
by: paul | last post by:
I have a JS function to change the width of a <divthat works great in Firefox, but not at all in IE7. In IE an error message occurs: Line: 92 Char: 5 Error: Invalid Argument Code: 0 Firefox...
7
by: Milenec | last post by:
Hi, so I was making this photo-gallery, and was using some javascript to make it look a little better, as in not having to refresh the entire page when you click on a thumbnail. I've gotten quite...
3
by: premprakashbhati | last post by:
hi, good evening.. i am going to upload an image in a web form .....for that iam using HTML input(file) control and one web control button i.e., Upload_Button() here is the code ...its work fine...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.