PDA

View Full Version : javascript - help please - again


[CN]ZooL
09-01-2001, 20:44
damn forums are totally messed up.. so heres what my first attempt should have said ;)

ok, dont know much javascript, so its a fair bet ive done something wrong or missed something huge from meh script ;)
but, heres what i have a problem with: (hope this makes sense)

1 image that id like to swap via choosing options on 2 seperate selects/lists. along lines of something like choice1 gives 'groupA' and choice2 gives 'week1' , then adding '.gif' to end to give src of the swapped image - groupAweek1.gif

(script i wrote is below) BUT. it doesnt work like i would have hoped. the principle works, in that the image is changed, but changed to an 'undefined' image, also, when loading the page in ie55 get the 'done, but errors' msg


<html>
<head>
<title>Untitled Document</title>
<script>
<!--

var where = "screenshots/"
var ImgNameGroup = document.theForm.groupChoice.value
var ImgNameWeek = document.theForm.weekChoice.value
var ImgType = ".gif"

function showScreenshot()
{
document.screenshotImg.src = where + ImgNameGroup + ImgNameWeek + ImgType
}

// -->
</script>
</head>

<body bgcolor="#000000" text="#999999">
<img src="screenshotImg.gif" name="screenshotImg">
<form name="theForm">
<table width="300" border="0" cellspacing="0" cellpadding="1">
<tr>
<td align="center" width="200">
<select name="groupChoice">
<option value="groupA" selected>Group A</option>
<option value="groupB">Group B</option>
<option value="groupC">Group C</option>
<option value="groupD">Group D</option>
<option value="groupE">Group E</option>
<option value="groupF">Group F</option>
<option value="groupG">Group G</option>
<option value="groupH">Group H</option>
</select>
</td>
<td align="center" width="200">
<select name="weekChoice" multiple onChange="showScreenshot()">
<option value="week1">Week 1</option>
<option value="week2">Week 2</option>
<option value="week3">Week 3</option>
<option value="week4">Week 4</option>
<option value="week5">Week 5</option>
</select>
</td>
</tr>
</table>
</form>
</body>
</html>

SPAWNO
09-01-2001, 23:10
um, me not know a huuuuuge amount of java, but surley, after you have constructed the filename of the .gif, you have to write it to the document ??

- S

[CN]ZooL
10-01-2001, 00:42
well, me not know much at all...but what i thought i was doing is (paraphrasing of course)

document.screenshotImg.src = where + ImgNameGroup + ImgNameWeek + ImgType find image with name=screenshotImg. and change the src to screenshots/groupAweek1.gif
with the new src value being taken from where + ImgNameGroup + ImgNameWeek + ImgType, and the variables set in the script.

but, obviously missed something important from the code :/
probably thinks the values are coming from a form already submitted, when i want it to take the values straight from the document.. :/

mr_grumpy_man
10-01-2001, 03:00
Damn I'm good... :E

Okay, sorted this out. Not through skill but by inspiration and trial and error...

Move the variable declarations so they're inside the showScreenshot() function :


function showScreenshot()
{
var where = "screenshots/"
var ImgNameGroup = document.theForm.groupChoice.value
var ImgNameWeek = document.theForm.weekChoice.value
var ImgType = ".gif"
document.screenshotImg.src = where + ImgNameGroup + ImgNameWeek + ImgType
alert(document.screenshotImg.src)
}


The ALERT command was what I used to debug what was going on. With the var declarations outside the function it was coming up with ImgNameGroup and ImgNameWeek being undefined... but putting the declarations inside the function they showed the correct values :)

Jinx M
10-01-2001, 15:46
yer, variables inside a function only "exist" inside that functioln and variables outside a function don't "exist" inside a function.

Dunno about javascript but with most languages you can define them as global making them available everywhere.

Couse, you could do away with the variables altogether:


function showScreenshot()
{
document.screenshotImg.src = "screenshots/" + document.theForm.groupChoice.value + document.theForm.weekChoice.value + ".gif"
}


Unless you actually need them of course. :E

You could also have just poked me in irc :)

[CN]ZooL
10-01-2001, 19:43
cheers guys,

i did write it with local variables to start with - in the function, didnt work, tried outside function, still didnt work

took away the variables, works fine now, cheers guys :D

[Edited by [CN]ZooL on 10-01-2001 at 06:57 PM]