Javascript help please! (Return/function question)

Miscellaneous Forums/General Discussion/Javascript help please! (Return/function question)

Commands like this:
document.write (selected)

require a semicolon at the end. Not sure if Javascript is tolerant enough, I don't think so. Make sure to turn on the javascript error msg console. If you don't, errors ill simply stop a script from working and you don't know why.

All commands lines require the semicolon, exceptions are single commands in if, for etc. brackets, eg:

for(i=0; i<100; i!!)
{ alert("I donßt need a semicolon") }

as well as if, function, for, while and so on structures.


Things like
var i=1;
selected=2;
all need it.

well your script is confusing me:

function change_b(selected)
{
selected=2;
return selected;
}


There is no sense in receiving the value of "selected" as a parameter since it has no influence on the return value.

So simply use

function change_b()
{
selected=2;
return selected;
}


But in this case you even donßt need to use functions at all, this should work as well:


<image src="image_a.bmp" onclick="selected=1;">
<image src="image_b.bmp" onclick="selected=2;">


And finally, your alert part won't work since it's not running in a loop, and loops are very uncommon in javascript. (msie even blocks them) Interactivity is rather realized using events and timeouts. Eg:

setTimeout(my_thread(),100);

will execute the function my_thread() after 100 millisecs.

Of course, the last line of the function my_thread() contains this line (setTimeout(my_thread(),100);) again, so the function will be called every 100 millisecs. This way you can have a parallel thread or "loop" that wont stop the browser.

You may do something like this:
var selected=0;
var old_selected=0;
setTimeout(my_thread(),1000);

function my_thread()
  {
  setTimeout(my_thread(),1000);
  if(selected != old_selected) 
  {
     alert('Value of selected is' + selected);
     old_selected=selected;
  }
}



Then again, I'm not sure if the alerts will block things if you dont click them away quickly.

Then there's a further thing that I am not sure: can you use onClick with images? Thought this works only with links? Images support OnMouseOver etc. not sure if OnClick is supported by all major browsers. Well you easily could add an <a> tag around them.