...

View Full Version : Switch with getElementsByTagName not working



Slyman007
08-06-2008, 11:42 AM
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:




<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

Kor
08-06-2008, 12:06 PM
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.


<!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>

Slyman007
08-07-2008, 12:06 AM
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

ninnypants
08-07-2008, 12:15 AM
You could use a regular expression


var wolf = /^wolfman/;
============
case "wolf.test(tat)":
txt="Wolfman is hairy.";
break;

Slyman007
08-07-2008, 02:20 AM
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...



<!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

ninnypants
08-07-2008, 02:53 AM
I gave you the wrong regular expression try these ones


var wolf = /wolfman/;
var godzilla = /Godzilla/;

And if that doesn't work try this


var wolf = new RegExp("wolfman");
var godzilla = new RegExp("godzilla");

Slyman007
08-07-2008, 07:22 AM
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.



<!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

Kor
08-07-2008, 09:12 AM
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:


<!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>

Slyman007
08-07-2008, 09:25 AM
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:



<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
08-07-2008, 09:28 AM
Ooh, Kor, your code is so much nicer than mine! I'll give that a shot!

Thanks!

D

Slyman007
08-07-2008, 09:48 AM
Kor, you're a genius!

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

Thanks a million!

Dave



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum