Hokay, so I have a website thats all working off of one static page, index.php. The content on the page is decided by the variables in the URL.
- index.php?site=home
- index.php?site=forum
- index.php?site=news
then I have a few that are a bit longer
- index.php?site=news&action=archive
I'm trying to make a script that will change the color of the nav button for the page that im currently on.
The script im working on gets the url of the page, uses .search to grab everything after ?. Then I string split and grab the second part (the part after the equal sign) , but this only works for the ones with 1 variable.
I was thinking something along the lines of an if statement checking to see if the parsed string has multiple "="s in it, and if it does grab the text after the last one. Something like that- idk I'm a total noob.
Also , is there anyway I can have the javascript setAttribute in the head, or does it need to be after the ID/class is declared?
<html>
<head>
<title>Test</title>
<style type="text/css">
.test {
color: yellow;
font-size: 5em;
}
</style>
<script type="text/javascript">
function parseUrl(url) {
var a = document.createElement('a');
a.href = url;
return a;
}
var page=parseUrl('').search;
var site=page.split("=")[1];
</script>
</head>
<body>
<ul>
<li id ="nav_home"><a href="testtest.html?site=home">Home</a>
<li id ="nav_forum"><a href="testtest.html?site=forum"/>Forum</a>
<li id ="nav_help"><a href="testtest.html?site=help"/>Help</a>
<li id ="nav_roster"><a href="testtest.html?site=roster"/>Roster<a/>
<li id ="nav_news"><a href="testtest.html?site=news"/>News<a/>
<li id ="nav_archive"><a href="testtest.html?site=news&action=archive"/>Archive<a/>
<script type="text/javascript">
document.getElementById("nav_" + site).setAttribute("class", "test");
</script>
</body>
</html>