Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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>
    Last edited by Kor; 08-06-2008 at 12:20 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • Users who have thanked Kor for this post:

    Slyman007 (08-07-2008)

  • #3
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    "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

  • #4
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    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 02:54 AM.

  • #5
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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

  • #6
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    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");

  • #7
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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

  • #8
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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>
    Last edited by Kor; 08-07-2008 at 09:59 AM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #9
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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

  • #10
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    array

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

    Thanks!

    D

  • #11
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Win

    Kor, you're a genius!

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

    Thanks a million!

    Dave


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •