Byte Calculator Using Javascript

Language

In this tutorial, I am going to teach you how to make a byte calculator using javascript. Just download the source code below and follow the instructions.

Directions

First we will write our html code.
  1. <form name="bandwidth" method="post">
  2. <p><input type="text" name="original" size="20" value=1024> <select size="1" name="units">
  3. <option value="Bytes">Bytes</option>
  4. <option value="Kb">Kb</option>
  5. <option value="Mb">Mb</option>
  6. <option value="Gb">Gb</option>
  7. </select> <input type="button" value="Calculate" name="B1" onClick="calculate()"></p>
  8. </form>
This will be our javacsript code.
  1. <script>
  2.  
  3. var bytevalue=0
  4. function calculate(){
  5. var invalue=document.bandwidth.original.value
  6. var selectunit=document.bandwidth.units.options[document.bandwidth.units.selectedIndex].value
  7. if (selectunit=="Bytes")
  8. bytevalue=invalue
  9. else if (selectunit=="Kb")
  10. bytevalue=invalue*1024
  11. else if (selectunit=="Mb")
  12. bytevalue=invalue*1024*1024
  13. else if (selectunit=="Gb")
  14. bytevalue=invalue*1024*1024*1024
  15.  
  16. alert (invalue+" "+selectunit+" is equal to:\n\n- "+bytevalue+" Bytes\n- "+Math.round(bytevalue/1024)+" Kb\n- "+Math.round(bytevalue/1024/1024)+" Mb\n- "+Math.round(bytevalue/1024/1024/1024)+" Gb\n")
  17. }
  18.  
  19. </script>
You have successfully created a simple byte calculator that you can use in your system. For more information, suggestions and questions just comment below or email me at [email protected]

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment