Byte Calculator Using Javascript

Submitted by: 
Language: 
Visitors have accessed this post 1048 times.

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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.