Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-06-2008, 10:42 AM   PM User | #1
Slyman007
New to the CF scene

 
Join Date: Aug 2008
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Slyman007 is an unknown quantity at this point
Switch with getElementsByTagName not working

Hi, all.

I'm trying to do something I thought would be relatively easy, but can't seem to get it to work.

I'd like to run a switch statement which gives a different result depending on what the html title of the page is.

I am using getElementsByTagName("title") which seems to work, as I can capture the page's title and put it into a variable and then write it, but once it goes into the switch statement, it always goes straight to the default, no matter what the title actually is, bypassing the other options.

I had the exact same problem using if statements instead of switch, but I think switch would be best when all is said and done, since I'll have multiple potential titles, so I'll post where I am with the switch statement.

Here's my sample code:

Code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Godzilla</title>
<script type="text/javascript">
{
var tit = document.getElementsByTagName("title"); 
var tat = (tit[0]);

document.write(tat.innerHTML);
document.write("  ----  ");

switch (tat) 
{
case "Frankenstein":
document.write("Frankenstein is green.");
break;
case "Wolfman":
document.write("Wolfman is hairy.");
break;
case "Godzilla":
document.write("Godzilla makes an awesome noise.");
break;
case "Dracula":
document.write("Dracula sucks.");
break;
default:
document.write("Why doesn't the Creature from the Black Lagoon get any love?");
}
}
</script> 

</head>

<body>

</body>

</html>
Any help would be greatly appreciated. I'm sure it's something rather simple I'm overlooking.

Dave
Slyman007 is offline   Reply With Quote
Old 08-06-2008, 11:06 AM   PM User | #2
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
Internet Explorer can not refer the <title> tag in DOM1 + manner with getElementsByTagName(). Could be an IE bug. Use the DOM0 crossbrowser syntax document.title instead.
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Godzilla</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
onload=function(){
var tit=document.title;
var txt;
switch (tit) 
{
case "Frankenstein":
txt="Frankenstein is green.";
break;
case "Wolfman":
txt="Wolfman is hairy.";
break;
case "Godzilla":
txt="Godzilla makes an awesome noise.";
break;
case "Dracula":
txt="Dracula sucks.";
break;
default:
txt="Why doesn't the Creature from the Black Lagoon get any love?";
}
document.getElementById('mydiv').innerHTML=txt;
}
</script>
</head>
<body>
<div id="mydiv"></div>
</body>
</html>
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

Last edited by Kor; 08-06-2008 at 11:20 AM..
Kor is offline   Reply With Quote
Users who have thanked Kor for this post:
Slyman007 (08-07-2008)
Old 08-06-2008, 11:06 PM   PM User | #3
Slyman007
New to the CF scene

 
Join Date: Aug 2008
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Slyman007 is an unknown quantity at this point
"contains" rather than = in switch

Huge thanks, Kor! Your revised code works wonderfully! I really appreciate your help!

I have an additional question, though. I may have coded myself into a corner.

Is there a way to take that same code, but search for whether the title CONTAINS a portion of text, rather than EQUALS the exact string?

So, if my title is "The Monster is Godzilla" can I make a case for that switch statement that looks to see if the title contains "Godzilla" versus another title that may read "The Monster is Wolfman?"

Thanks for all your help!

Warmly,

Dave
Slyman007 is offline   Reply With Quote
Old 08-06-2008, 11:15 PM   PM User | #4
ninnypants
Regular Coder

 
ninnypants's Avatar
 
Join Date: Apr 2008
Location: Utah
Posts: 504
Thanks: 10
Thanked 47 Times in 47 Posts
ninnypants is an unknown quantity at this point
You could use a regular expression
Code:
var wolf = /^wolfman/;
============
case "wolf.test(tat)":
txt="Wolfman is hairy.";
break;

Last edited by ninnypants; 08-07-2008 at 01:54 AM..
ninnypants is offline   Reply With Quote
Old 08-07-2008, 01:20 AM   PM User | #5
Slyman007
New to the CF scene

 
Join Date: Aug 2008
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Slyman007 is an unknown quantity at this point
regular expression test

Thanks for your suggestion, Ninnypants. However, it doesn't seem to work for me in my code.

Regular expressions scare me, though, so I probably am just missing something simple. But, when I inserted your suggested code, I have the same problem as I did before, it jumps straight to the default, even if the title has the correct string in it.

Hopefully, you'll see that I just missed an important tag somewhere to make regexps work...

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Godzilla</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
onload=function(){
var tit=document.title;
var txt;
var wolf = /^wolfman/;
var godzilla = /^Godzilla/;

switch (tit) 
{
case "Frankenstein":
txt="Frankenstein is green.";
break;
case "wolf.test(tit)":
txt="Wolfman is hairy.";
break;
case "godzilla.test(tit)":
txt="Godzilla makes an awesome noise.";
break;
case "Dracula":
txt="Dracula sucks.";
break;
default:
txt="Why doesn't the Creature from the Black Lagoon get any love?";
}
document.getElementById('mydiv').innerHTML=txt;
}
</script>
</head>
<body>
<div id="mydiv"></div>
</body>
</html>
Dave
Slyman007 is offline   Reply With Quote
Old 08-07-2008, 01:53 AM   PM User | #6
ninnypants
Regular Coder

 
ninnypants's Avatar
 
Join Date: Apr 2008
Location: Utah
Posts: 504
Thanks: 10
Thanked 47 Times in 47 Posts
ninnypants is an unknown quantity at this point
I gave you the wrong regular expression try these ones
Code:
var wolf = /wolfman/;
var godzilla = /Godzilla/;
And if that doesn't work try this
Code:
var wolf = new RegExp("wolfman");
var godzilla = new RegExp("godzilla");
ninnypants is offline   Reply With Quote
Old 08-07-2008, 06:22 AM   PM User | #7
Slyman007
New to the CF scene

 
Join Date: Aug 2008
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Slyman007 is an unknown quantity at this point
regular expressions not working

Thanks again, Ninnypants, but, sadly, the code still doens't seem to work with the regular expressions. And, since I know so little about them, I feel helpless in troubleshooting.

I tried one of each of your suggestions in this code. The switch works if the value is identical on the two non-regular-expression cases. But, with the regexs, it just goes straight to default.

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Godzilla is Green</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
onload=function(){
var tit=document.title;
var txt;
var wolf = /wolfman/;
var godzilla = new RegExp("godzilla");

switch (tit) 
{
case "Frankenstein":
txt="Frankenstein is green.";
break;
case "wolf.test(tit)":
txt="Wolfman is hairy.";
break;
case "godzilla.test(tit)":
txt="Godzilla makes an awesome noise.";
break;
case "Dracula":
txt="Dracula sucks.";
break;
default:
txt="Why doesn't the Creature from the Black Lagoon get any love?";
}
document.getElementById('mydiv').innerHTML=txt;
}
</script>
</head>
<body>
<div id="mydiv"></div>
</body>
</html>
Sorry to be a bugger.

D
Slyman007 is offline   Reply With Quote
Old 08-07-2008, 08:12 AM   PM User | #8
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
You may use an array (or better an object) to create a relationship between the keyword and the message, and use a for loop or a while loop instead of a switch/case statement:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Godzilla</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
onload=function(){
var tit=document.title;
var txt="Why doesn't the Creature from the Black Lagoon get any love?";//default message
var titObj={
'Frankenstein':'Frankenstein is green.',
'Wolfman':'Wolfman is hairy.',
'Godzilla':'Godzilla makes an awesome noise.',
'Dracula':'Dracula sucks.'
}
var reg;
for(prop in titObj){
reg=new RegExp(prop,'i');
reg.test(tit)?txt=titObj[prop]:null;
}
document.getElementById('mydiv').innerHTML=txt;
}
</script>
</head>
<body>
<div id="mydiv"></div>
</body>
</html>
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

Last edited by Kor; 08-07-2008 at 08:59 AM..
Kor is offline   Reply With Quote
Old 08-07-2008, 08:25 AM   PM User | #9
Slyman007
New to the CF scene

 
Join Date: Aug 2008
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Slyman007 is an unknown quantity at this point
Workaround

Well, it's messy as hell, but I was able to re-write everything and use a bunch of if statements to accomplish my goal. So, it's ugly code, but will work for what I'm doing.

If you're curious, here's what seems to work for me:

Code:
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>I've heard that Dracula bites</title>
<script type="text/javascript">
onload=function(){
var tit=document.title;
var wo = /wolf/i;
var go = /godzilla/i;
var dr = /dracula/i;

if (wo.test(tit))
document.write("Wolfman for the win");
else if (go.test(tit))
document.write("Godzilla for the win");
else if (dr.test(tit))
document.write("Dracula for the win");
else
document.write("Nobody wins");
}
</script>

</head>

<body>

</body>

</html>
I'll have a ton of If statements by the time I'm done, which is why I was trying to go with the switch, but at this point, I just want to have it done.

Thanks for your help!

Dave
Slyman007 is offline   Reply With Quote
Old 08-07-2008, 08:28 AM   PM User | #10
Slyman007
New to the CF scene

 
Join Date: Aug 2008
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Slyman007 is an unknown quantity at this point
array

Ooh, Kor, your code is so much nicer than mine! I'll give that a shot!

Thanks!

D
Slyman007 is offline   Reply With Quote
Old 08-07-2008, 08:48 AM   PM User | #11
Slyman007
New to the CF scene

 
Join Date: Aug 2008
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Slyman007 is an unknown quantity at this point
Win

Kor, you're a genius!

Your new array code works brilliantly. And, SO much easier to add new conditions.

Thanks a million!

Dave
Slyman007 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 04:01 PM.


Advertisement
Log in to turn off these ads.