Digital Point Forums
Send Telegram

Go Back   Digital Point Forums > Design & Development > Programming > JavaScript
Google Analytics
Log In to view
your analytics

Reply
 
Thread Tools
  #1  
Old Mar 27th 2005, 1:40 pm
fraser5002 fraser5002 is offline
Peon
 
Join Date: Mar 2005
Posts: 10
fraser5002 is on a distinguished road
little help with switch case staement

hi the user can change a selected image by choosing an option from a menu ive got that complete but what i want to do now is when the user clicks that image something happens for each different image i am trying to use the following code to do this but its giving me problems throwing me errors. is it way off or just need a wee bit of tweaking?

Code:
<img border="2" src="Buchaille_Etive_Mor.jpg" name="bigpic" align="top" width="120" height="90"
onclick ="
picture = (bigpic.src)
<SCRIPT LANGUAGE="JavaScript">
switch (picture)
 {
   case "aonach_egach.jpg": 
      alert("still")
      break;
  
   default : 
   alert("still 2")
 }
 </SCRIPT>
">
i will add more case statements once i get it up and running

thanks
Reply With Quote
  #2  
Old Mar 27th 2005, 1:53 pm
nullbit nullbit is offline
Hand of A'dal
 
Join Date: Feb 2005
Posts: 489
nullbit is on a distinguished road
You should do something like this:

HTML:
Code:
<img [...] onclick="imageSwitch(this.src)" />
JS:
Code:
<script type="text/javascript">
<!--
function imageSwitch(src)
{
  // switch statement goes here
}
// -->
</script>
__________________
SEO Tool - The killer search engine optimization tool. No. Really.
The Search Engine Experiment - Discover if Google really giving you the most relevant results
- No recip required.
Reply With Quote
  #3  
Old Mar 27th 2005, 2:27 pm
fraser5002 fraser5002 is offline
Peon
 
Join Date: Mar 2005
Posts: 10
fraser5002 is on a distinguished road
hi thnaks for your reply i have changed it to what you said

Code:
<img border="2" src="Buchaille_Etive_Mor.jpg" name="bigpic" align="top" width="120" height="90" onclick="imageSwitch(bigpic.src)">

<script type="text/javascript">
<!--
function imageSwitch(src)
{
case "aonach_egach.jpg": 
      alert("still")
      break;
  
   default : 
   alert("still 2")
}
// -->
</script>
but its still giving me an error what am i doing wrong?
Reply With Quote
  #4  
Old Mar 27th 2005, 2:37 pm
nullbit nullbit is offline
Hand of A'dal
 
Join Date: Feb 2005
Posts: 489
nullbit is on a distinguished road
You had a few errors, try this revision:

Code:
<img border="2" src="Buchaille_Etive_Mor.jpg" name="bigpic" align="top" width="120" height="90" onclick="imageSwitch(this.src)">

<script type="text/javascript">
<!--
function imageSwitch(src)
{
  switch(src)
  {
  case "aonach_egach.jpg": 
    alert("still");
    break;
  default: 
    alert("still 2");
  }
}
// -->
</script>
__________________
SEO Tool - The killer search engine optimization tool. No. Really.
The Search Engine Experiment - Discover if Google really giving you the most relevant results
- No recip required.
Reply With Quote
  #5  
Old Mar 27th 2005, 2:39 pm
fraser5002 fraser5002 is offline
Peon
 
Join Date: Mar 2005
Posts: 10
fraser5002 is on a distinguished road
ahhh thanks (complete nooby if you hadnt worked that out)

cheers again
Reply With Quote
  #6  
Old Mar 27th 2005, 2:42 pm
fraser5002 fraser5002 is offline
Peon
 
Join Date: Mar 2005
Posts: 10
fraser5002 is on a distinguished road
hmmm actually one more question it now seems to always be saying the case else statment "still 2" all the time even if the right image is showing i know the filename aonach_egach.jpg is definiatley correct
in your code where it says (this.src) i put in my name of my image "bigpic" in there yeh ? so itll be (bigpic.src) thats still giving the problem above but just wondered if that should be done anyway?

thanks for you time

Last edited by fraser5002; Mar 27th 2005 at 3:03 pm.
Reply With Quote
  #7  
Old Mar 27th 2005, 3:20 pm
J.D. J.D. is offline
of the Nightfall
 
Join Date: Nov 2004
Posts: 1,198
J.D. has a spectacular aura aboutJ.D. has a spectacular aura about
Quote:
Originally Posted by fraser5002
i put in my name of my image "bigpic" in there yeh ? so itll be (bigpic.src) thats still giving the problem above but just wondered if that should be done anyway?
You need to learn to use a good advice. this.src is not the same as bigpic.src. Run the code nullbit gave and once you see that it works (and it will), then try to work your way to whatever modification you want to implement based on this working example.

J.D.
Reply With Quote
  #8  
Old Mar 27th 2005, 3:24 pm
fraser5002 fraser5002 is offline
Peon
 
Join Date: Mar 2005
Posts: 10
fraser5002 is on a distinguished road
it does work but theres just one small problem it always goes to the default option of the switch statement but i dont know why as the case statements im giving the function are definetly correct.
ive tried it with lots of different images all doing the same thing.
Reply With Quote
  #9  
Old Mar 27th 2005, 3:29 pm
J.D. J.D. is offline
of the Nightfall
 
Join Date: Nov 2004
Posts: 1,198
J.D. has a spectacular aura aboutJ.D. has a spectacular aura about
Quote:
Originally Posted by fraser5002
it does work but theres just one small problem it always goes to the default option of the switch statement but i dont know why as the case statements im giving the function are definetly correct.
ive tried it with lots of different images all doing the same thing.
It's because this.src is a fully-qualified path. You need either to change your case statement or pass image name or ID instead of the src attribute. Add this code to see for yourself:

function imageSwitch(src)
{
alert(src);
switch(src)
...

I would change this code to use name/ID instead.

J.D.
Reply With Quote
  #10  
Old Mar 27th 2005, 3:35 pm
fraser5002 fraser5002 is offline
Peon
 
Join Date: Mar 2005
Posts: 10
fraser5002 is on a distinguished road
thanks for the advice i see what you mean now but the way i have set it up so that when the user changes the image the picture has the same name just a different src if you know what i mean?

like user clicks a hyperlink bigpic.src = "blabla"

so i dont think i can use youe method of passing out the image name ( mayby im on the wrong end of the stick )

i know in VB you can strip off part of a filename just to reveal the last part something.jpg can this be done in Javascript?

sorry if my terminology is way off
Reply With Quote
  #11  
Old Mar 27th 2005, 3:43 pm
J.D. J.D. is offline
of the Nightfall
 
Join Date: Nov 2004
Posts: 1,198
J.D. has a spectacular aura aboutJ.D. has a spectacular aura about
Quote:
Originally Posted by fraser5002
i know in VB you can strip off part of a filename just to reveal the last part something.jpg can this be done in Javascript?
It's easy in JS:

src = src.replace(new RegExp("^.*/([^\\.]+\\.[a-z]+)$", "i"), "$1");

J.D.
Reply With Quote
  #12  
Old Mar 27th 2005, 3:56 pm
fraser5002 fraser5002 is offline
Peon
 
Join Date: Mar 2005
Posts: 10
fraser5002 is on a distinguished road
thanks for your help
Quote:
src = src.replace(new RegExp("^.*/([^\\.]+\\.[a-z]+)$", "i"), "$1");
p.s thats the weirdest looking thing ive ever seen dont have a scooby what it means but does the job.
Reply With Quote
  #13  
Old Mar 27th 2005, 4:15 pm
J.D. J.D. is offline
of the Nightfall
 
Join Date: Nov 2004
Posts: 1,198
J.D. has a spectacular aura aboutJ.D. has a spectacular aura about
Quote:
Originally Posted by fraser5002
thanks for your help

p.s thats the weirdest looking thing ive ever seen dont have a scooby what it means but does the job.
It's called regular expression. It's really powerful and works like this:

Code:
^   : start at the beginning of the line
.*  : zero or more (*) occurrences of any character (.)
/   : (/)
(   : begin a group that may be later references by $1
[^\\.]+  : one or more (+) occurence of any character except (.)
\\.  : escaped (.)
[a-z]+  : one or more (+) characters from (a) to (z)
)   : end of the group
$   : end of the line
The second argument to replace is $1, which is assigned by JS to whatever is between the first group of parenthesis (you can have more than one group).

J.D.
Reply With Quote
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
switch ur hotmail into 250 mb mohamad1983 General Chat 13 Jan 22nd 2008 4:35 pm
About Switch Statement (php) Nasimov PHP 11 Oct 27th 2005 2:18 pm
Google and the Mysterious Case of the 1969 Pagejackers vagrant Google 25 Feb 15th 2005 10:37 am
Micro-SaP's threat, switch to XP or? anthonycea General Chat 10 Sep 27th 2004 5:02 pm
Are SE's case sensitive to meta tags? harish318 Search Engine Optimization 2 Jun 26th 2004 12:05 am


All times are GMT -8. The time now is 8:48 pm.