473,486 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Set image onclick and keep it there

Hi All,

I am hoping someone can help me. I am trying to setup my main page so that
when the user moves the mouse over an image, it changes the source (got this
working). When the user CLICKS on the image (it is a link to the content
page) it changes to a third image AND STAYS THERE. ie: so the iamge won't
change back to the omouseoff image. Does this make sense?

Also, how do I "preload" the images before they are even needed? eg: so the
Loading image loads immedeately.

Here is the code I have so far, which works as far is it goes, however the
"Loading" image is overwritten by the mouse off function. What am I doing
wrong? (Sorry for formatting):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<script id="clientEventHandlersJS" language="javascript">
<!--

var clicked='false';

function fncover(){
if (clicked='false')
{
IMG1.src='./images/webmainlogoover.gif';
}
}

function fncoff(){
if (clicked='false')
{
IMG1.src='./images/webmainlogo.gif';
}
}

function fncclick(){
IMG1.src='./images/Loading.gif';
clicked='true';
}

//-->
</script>
</head>
<body>
<a href="Content.aspx" target="maintitle">
<img
onmouseover="return fncover()"
onmouseout="return fncoff()"
onclick="return fncclick()"
src="./images/webmainlogo.gif"
border="0"
id="IMG1"
language="javascript"
width="500" >
</a>
</body>
</html>

Thanx in advance,

Rob
ro**@devtest.com
Jul 20 '05 #1
2 4414
On Sun, 18 Jan 2004 16:56:39 GMT, Rob Manger <ro********@hotmail.com>
wrote:

<snip>
Also, how do I "preload" the images before they are even needed? eg: so
the Loading image loads immedeately.
If I recall correctly:

var img = new Image();
img.src = 'image1.gif'; // Preload image1.gif

What I don't remember is if you should use an array of Image objects, each
preloading a separate image, or if you can use one. Try it and see. :)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
Nice to see someone using a DTD for a change. :)
<html>
<head>
<script id="clientEventHandlersJS" language="javascript">
The SCRIPT element doesn't have an id attribute. Also, it requires (that
is, it's mandatory) a type attribute. The above should read:

<script type="text/javascript">

When the type attribute is used, there is no need to use the language
attribute.
<!--
Don't bother with SGML comments in SCRIPT elements. They're no longer
required.
var clicked='false';
Why are you using a string to indicate true or false? That's what booleans
are for. Instead use:

var clicked = false;

This also allows you to write

if ( clicked ) {

rather than

if ( clicked == 'false' ) {

which is another way of solving the next issue...
function fncover(){
if (clicked='false')
This is where your problems lie. When you assign a value to a variable,
the expression returns the assigned value. That is, if you did this:

window.alert( clicked = 'false' );

the alert box would display, false. As this returned value is not null,
undefined, false, 0, or an empty string (all considered false), your if
expression is *always* true. Instead, use:

if ( 'false' == clicked ) {

Note: I reversed the order of the expression out of habit. If you wrote,

if ( 'false' = clicked ) {

you would get an error as you can't assign to a literal value. Placing
constants on the left-hand side when you are performing a comparison can
help avoid these simple mistakes.
{
IMG1.src='./images/webmainlogoover.gif';
This might not work on all browsers because IMG1 is not a global, it's the
id of an element. There are two solutions in your case; getElementById()
or passing an object reference. The latter is easier, so I'll just show
that.

Inside an intrinsic event, such as onclick, the keyword 'this' is a
reference the object that was clicked. You can then use it to access the
properties of that object. For example:

function myClick( elem ) {
elem.src = '...';
}
...
<img src="..." onclick="myClick(this)">

<snip>
<img
onmouseover="return fncover()"
onmouseout="return fncoff()"
onclick="return fncclick()"
src="./images/webmainlogo.gif"
border="0"
id="IMG1"
language="javascript"
width="500" >


The IMG element doesn't have a language attribute. To specify the language
used in intrinsic events, place this META element in the head of the
document.

<meta http-equiv="Content-Script-Type" content="text/javascript">
SUMMARY - thought I'd better do this, for clarity

Use SCRIPT elements like

<script type="text/javascript">
Don't use incompatible globals - pass references to your functions instead.
Use booleans, not strings (where appropriate).
Make sure you compare, not assign.

function fncover( elem ) {
if ( true == clicked ) { // or more simply: if ( clicked ) {
elem.src = 'newImage.gif';
}
}
...
<img
onmouseover="fncover(this)"
onmouseout="fncoff(this)"
onclick="fncclick(this)"
src="./images/webmainlogo.gif"
border="0"
id="IMG1"
width="500">

One last thing: you'll notice above that I removed the return keyword in
the event attributes of the IMG element. This is because you never
returned a value from any of the functions. As I don't remember anything
about you needing to return a value, in this case, I didn't add return
statements.

Hope that helps,

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #2
Excellent!!!! Thanx Mike. Your a legend :D

Rob
Jul 20 '05 #3

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

Similar topics

3
63699
by: -DRB- | last post by:
I suspect this is fairly standard practice, but I've looked on the net and can't quite find the script I want. I would like to have a page with an image on which changed depending on which link...
5
2124
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...
14
11016
by: D. Alvarado | last post by:
Hello, I am trying to open a window containing an image and I would like the image to be flush against the window -- i.e. have no padding or border. Can I make this happen with a single call to a...
1
2490
by: John Thompson | last post by:
We're sooo close. When we load the page to upload the image, all of the prms go through except the binary image data. Using SQL server with the data type set to "image". Please help! Thanks-...
3
3303
by: lofty00 | last post by:
hello, sorry about the repost - I've been posting to several groups and I've decided it's better to make a single repost to all of them rather than an extra post in each. I've been trying to...
7
2423
by: simchajoy2000 | last post by:
Hi, I am just a javascript beginner so maybe this is a simple problem but I am trying to do some rollovers on images in a separate <div>. Here is the relevent piece of my code: <html>...
10
6734
by: cjparis | last post by:
Hello everyone. If anyone can give me a hand I would be gratefull Am doing a site which requires a moving element and have used DHTML to do it. Have a simple Browser detect script to sort IE...
1
4061
by: sourcie | last post by:
I am changing an existing quiz found on "JavaScriptKit.com Multiple Choice Quiz" I have an image. Instead of using the radio buttons with the normal true/false question, I want to place two...
3
4400
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
6964
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
7123
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,...
1
6842
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
7319
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4864
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...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3069
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
262
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.