Solved

javascript syntax

Posted on 2012-04-08
6
209 Views
Last Modified: 2012-04-09
Hello Experts,
I am trying to make a two part menu in this script a different section of the menu shows a different div in a different color depending on what page the user is on. I am having a bit of trouble getting the syntax right. Can you take a look?
Thanks,
rhyno99

window.onload=function(){
document.getElementById("menu2").style.display="none"
      if (window.location.toString().indexOf("index") > -1) {
      document.getElementById("welcome").style.display="block";
      document.getElementById("menu2").css background-color:'#F00';
      }
      else if (window.location.toString().indexOf("K-2") > -1) {
      document.getElementById("catk-2").style.display="block";
      document.getElementById("menu2").css background-color:'#F90';
      }
      else if (window.location.toString().indexOf("3-5") > -1) {
      document.getElementById("cat3-5").style.display="block";
      document.getElementById("menu2").css background-color:'#06F';
      }
      else if (window.location.toString().indexOf("6-8") > -1) {
      document.getElementById("cat6-8").style.display="block";
      document.getElementById("menu2").css background-color:'#0F0';
      }
      else {
      else if (window.location.toString().indexOf("9-12") > -1) {
      document.getElementById("cat9-12").style.display="block";
      document.getElementById("menu2").css background-color:'#FF0';
      }
      
      else {
      document.getElementById("welcome").style.display="block";
      document.getElementById("menu2").css background-color:'#F00';
      }
}
0
Comment
Question by:rhyno99
6 Comments
 
LVL 2

Assisted Solution

by:Artic
Artic earned 100 total points
Comment Utility
You had two else statements. Try this:

window.onload=function(){
document.getElementById("menu2").style.display="none"
      if (window.location.toString().indexOf("index") > -1) {
      document.getElementById("welcome").style.display="block";
      document.getElementById("menu2").css background-color:'#F00';
      }
      else if (window.location.toString().indexOf("K-2") > -1) {
      document.getElementById("catk-2").style.display="block";
      document.getElementById("menu2").css background-color:'#F90';
      }
      else if (window.location.toString().indexOf("3-5") > -1) {
      document.getElementById("cat3-5").style.display="block";
      document.getElementById("menu2").css background-color:'#06F';
      }
      else if (window.location.toString().indexOf("6-8") > -1) {
      document.getElementById("cat6-8").style.display="block";
      document.getElementById("menu2").css background-color:'#0F0';
      }
      else if (window.location.toString().indexOf("9-12") > -1) {
      document.getElementById("cat9-12").style.display="block";
      document.getElementById("menu2").css background-color:'#FF0';
      }
      else {
      document.getElementById("welcome").style.display="block";
      document.getElementById("menu2").css background-color:'#F00';
      }
}
0
 

Author Comment

by:rhyno99
Comment Utility
Thanks, whoops that was in copying. Here is what I have but it still doesn't work.

window.onload=function(){
document.getElementById("menu2").style.display="none"
      if (window.location.toString().indexOf("index") > -1) {
      document.getElementById("welcome").style.display="block";
      document.getElementById("menu2").css background-color:'#F00';
      }
      else if (window.location.toString().indexOf("K-2") > -1) {
      document.getElementById("catk-2").style.display="block";
      document.getElementById("menu2").css background-color:'#F90';
      }
      else if (window.location.toString().indexOf("3-5") > -1) {
      document.getElementById("cat3-5").style.display="block";
      document.getElementById("menu2").css background-color:'#06F';
      }
      else if (window.location.toString().indexOf("6-8") > -1) {
      document.getElementById("cat6-8").style.display="block";
      document.getElementById("menu2").css background-color:'#0F0';
      }
      else if (window.location.toString().indexOf("9-12") > -1) {
      document.getElementById("cat9-12").style.display="block";
      document.getElementById("menu2").css background-color:'#FF0';
      }
      else {
      document.getElementById("welcome").style.display="block";
      document.getElementById("menu2").css background-color:'#F00';
      }
}
0
 
LVL 23

Accepted Solution

by:
basicinstinct earned 350 total points
Comment Utility
the syntax is this:

document.getElementById("menu2").style.backgroundColor = "#F00";
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 20

Assisted Solution

by:Proculopsis
Proculopsis earned 50 total points
Comment Utility
Try using a switch to make your code more readable:

switch (document.location.href.replace(/.*(index|k-2|3-5|6-8|9-12).*/, "$1")) {
    case "index":
        // code here
        break;

    case "k-2":
        // code here
        break;

    case "3-5":
        // code here
        break;

    case "6-8":
        // code here
        break;

    case "9-12":
        // code here
        break;

    default:
        // code here
        break;
}

Open in new window

0
 
LVL 2

Assisted Solution

by:Artic
Artic earned 100 total points
Comment Utility
Needs to be style.backGroundColor not .css background-color. Try this:

window.onload=function(){
document.getElementById("menu2").style.display="none";
      if (window.location.toString().indexOf("index") > -1) {
      document.getElementById("welcome").style.display="block";
      document.getElementById("menu2").style.backgroundColor = "#F00";
      }
      else if (window.location.toString().indexOf("K-2") > -1) {
      document.getElementById("catk-2").style.display="block";
      document.getElementById("menu2").style.backgroundColor = "#F90";
      }
      else if (window.location.toString().indexOf("3-5") > -1) {
      document.getElementById("cat3-5").style.display="block";
      document.getElementById("menu2").style.backgroundColor = "#06F";
      }
      else if (window.location.toString().indexOf("6-8") > -1) {
      document.getElementById("cat6-8").style.display="block";
      document.getElementById("menu2").style.backgroundColor = "#0F0";
      }
      else if (window.location.toString().indexOf("9-12") > -1) {
      document.getElementById("cat9-12").style.display="block";
      document.getElementById("menu2").style.backgroundColor = "#FF0";
      }
      else {
      document.getElementById("welcome").style.display="block";
      document.getElementById("menu2").style.backgroundColor = "#F00";
      }
};
0
 

Author Closing Comment

by:rhyno99
Comment Utility
Thank you all
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now