Simple, Simple Javascript question...
-
making a menu I want to have the option of the current year, the 2 proceeding it and the next one... this very very simple bit of code does the job well, I'm just wondering if there's any reason I should use an array instead of separate variables or if I'm breaking any other good practice rules... <script> d = new Date(); c = d.getFullYear(); a = c - 2; b = c - 1; d = c + 1; </script> and then further down, the menu <ul> <li><script>document.write('<a class="btn" href="report.php?y=' + a + '">' + a + '</a>');</script></li> <li><script>document.write('<a class="btn" href="report.php?y=' + b + '">' + b + '</a>');</script></li> <li><script>document.write('<a class="btn" href="report.php?y=' + c + '">' + c + '</a>');</script></li> <li><script>document.write('<a class="btn" href="report.php?y=' + d + '">' + d + '</a>');</script></li> </ul>
-
making a menu I want to have the option of the current year, the 2 proceeding it and the next one... this very very simple bit of code does the job well, I'm just wondering if there's any reason I should use an array instead of separate variables or if I'm breaking any other good practice rules... <script> d = new Date(); c = d.getFullYear(); a = c - 2; b = c - 1; d = c + 1; </script> and then further down, the menu <ul> <li><script>document.write('<a class="btn" href="report.php?y=' + a + '">' + a + '</a>');</script></li> <li><script>document.write('<a class="btn" href="report.php?y=' + b + '">' + b + '</a>');</script></li> <li><script>document.write('<a class="btn" href="report.php?y=' + c + '">' + c + '</a>');</script></li> <li><script>document.write('<a class="btn" href="report.php?y=' + d + '">' + d + '</a>');</script></li> </ul>
It's neither here nor there if you store the numbers in variables or use them in-line, however your choice of variable names is definitely against good practice. Call your variables things like previousYear, thisYear and nextYear. If you're looking to try new things you could also use a "for" loop to write your links rather than doing it as you are. That way you can alter how many years back\forward you display rather than having to show four.
-
It's neither here nor there if you store the numbers in variables or use them in-line, however your choice of variable names is definitely against good practice. Call your variables things like previousYear, thisYear and nextYear. If you're looking to try new things you could also use a "for" loop to write your links rather than doing it as you are. That way you can alter how many years back\forward you display rather than having to show four.
heh, wasn't too fussed about conventions like variable naming, was more interested in if there's any reason why declaring and assigning variables like that and then just referencing them in the spot I needed would be bad practice. I think if I was gonna make a loop for it, I'd change the page from being html to php, was really just a quick exercise in me just trying to keep it simple and keep the page html rather than php