Forum Moderators: open

Message Too Old, No Replies

Help! My javascript won't work

New to javascript...can't get this working...

         

margolane

9:54 pm on May 13, 2009 (gmt 0)

10+ Year Member



Help! Can one of you wizards out there fix my javascript for me please. For the life of me, I can't figure out why it's not working.

I have two separate buttons created called chipinprog.jpg and chipinprogdown.jpg.

Thanks!

<a href="chipin.html"
onmouseover="buttondown('chipinprogdownbutton')"
onmouseout="buttonup('chipinprogbutton')">
<img src="chipinprog.jpg" name="chipinprogbutton" border="0" />
</a>

</STYLE>
<script language="JavaScript" type="text/javascript">
<!--
if (document.images) {
chipinprogbuttonup = new Image();
chipinprogbuttonup.src = "chipinprog.jpg" ;
chipinprogbuttondown = new Image() ;
chipinprogbuttondown.src = "chipinprogdown.jpg" ;
}
function buttondown( chipinprogbuttondown )
{
if (document.images) {
document[ chipinprogbutton ].src = eval( chipinprogbutton + "down.src" );
}
}
function buttonup ( chipinprogbutton )
{
if (document.images) {
document[ chipinprogbutton ].src = eval( chipinprogbutton + "up.src" );
}
}
// -->
</script>

daveVk

2:17 am on May 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<a href="chipin.html"
onmouseover="this.getElementById('chipinprogbutton').src=chipinprogbuttondown.src"
onmouseout="this.getElementById('chipinprogbutton').src=chipinprogbuttonup.src">
<img src="chipinprog.jpg" name="chipinprogbutton" border="0" />
</a>

<script language="JavaScript" type="text/javascript">
<!--
if (document.images) {
var chipinprogbuttonup = new Image();
chipinprogbuttonup.src = "chipinprog.jpg" ;
var chipinprogbuttondown = new Image() ;
chipinprogbuttondown.src = "chipinprogdown.jpg" ;
}

// -->
</script>

astupidname

2:40 am on May 14, 2009 (gmt 0)

10+ Year Member



Or another variation:
<a href="chipin.html"
onmouseover="setImage('chipinprogbutton','chipinprogdown.jpg')"
onmouseout="setImage('chipinprogbutton','chipinprog.jpg')">
<img src="chipinprog.jpg" alt="pic" id="chipinprogbutton" border="0" />
</a>

<script type="text/javascript">
//we don't actually need to pre-load 'chipinprog.jpg', as it is defined in an img tag already
//and will already be cached, but I am including it below in the
//imagesToPreload array just for demo
var imagesToPreload = ['chipinprog', 'chipinprogdown.jpg']; //add any other image paths in to this array that you wish to be preloaded
var preloadedImages = []; //a storage array for the 'for' loop to use below for new Image's
for (var i = 0; i < imagesToPreload.length; i++) {
preloadedImages[i] = new Image();
preloadedImages[i].src = imagesToPreload[i];
}

//one function is all that is needed.
//pass images id as first parameter and the new src value as second parameter
function setImage(imgElementId,newSrc) { //use id in the image, and document.getElementById instead
document.getElementById(imgElementId).src = newSrc;
}

</script>

margolane

3:36 am on May 14, 2009 (gmt 0)

10+ Year Member



None of this makes any more sense to me than the script I submitted. Remember, I'm a newby. Your scripts have just confused me even more.

daveVk

7:18 am on May 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are numerous problems with your code eg

function buttondown( chipinprogbuttondown )
{
if (document.images) {
document[ chipinprogbutton ].src = eval( chipinprogbutton + "down.src" );
}
}

eval( chipinprogbutton + "down.src" );

chipinprogbutton is undefined, perhaps chipinprogbuttondown ahould be chipinprogbutton as per buttonup() ?

as you add chipinprogbutton to a text string ( + "down.src" ) assumably it needs to be a text string ?

there is no need to eval() a text string

document[ chipinprogbutton ].src

whats in the [] needs to be a text string
do you mean

document[ "chipinprogbutton" ].src
equiv to
document.chipinprogbutton.src

if so

document.getElementById( "chipinprogbutton" ).src is preferable

Etc

margolane

6:12 pm on May 14, 2009 (gmt 0)

10+ Year Member



Nope, sorry. You guys have all lost me. Your terms are too technical.

What I really needed was someone to just take the code, put in all the right words, and fix it for me.

Thanks for trying.

DrDoc

6:15 pm on May 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You have at least two fixed versions above ... :)

margolane

7:28 pm on May 14, 2009 (gmt 0)

10+ Year Member



...yes, but they don't mean anything to me. Those fixed versions don't have my chipinprog put in the right place. They just have a bunch of terms and words I don't understand.

Sorry.

DrDoc

8:51 pm on May 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, as long as they work, it should hardly matter if they appear differen from what you already have, right? Yours doesn't work right now anyway ... so a working (albeit different) script should be okay.

margolane

9:05 pm on May 14, 2009 (gmt 0)

10+ Year Member



Looking back over them, is the second entry a working version? Can I just cut and paste it?

DrDoc

9:12 pm on May 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, that should work :)