Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. Simple, Simple Javascript question...

Simple, Simple Javascript question...

Scheduled Pinned Locked Moved JavaScript
questionjavascriptphpdata-structurestools
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TenmanS14
    wrote on last edited by
    #1

    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>

    F 1 Reply Last reply
    0
    • T TenmanS14

      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>

      F Offline
      F Offline
      F ES Sitecore
      wrote on last edited by
      #2

      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.

      T 1 Reply Last reply
      0
      • F F ES Sitecore

        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.

        T Offline
        T Offline
        TenmanS14
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups