sersha
Codebase Quality Auditor
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
300 XP
In this tutorial we will create a File Size Converter using JavaScript. This code will calculate the given file size when the user click the button. The code use onclick() to initiate the calculateSize() function that compute the actual size of the given value by multiplying it or by dividing it by 1024 . This is a free program, you can modify it and use it as your own.
We will be using JavaScript as a server-side scripting language because It allows greater control of your web page behavior than HTML alone. It is embedded in HTML that responsible to allow user to interact with the computer .
Getting started:
First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.
The Main Interface
This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
Creating the Script
This code contains the script of the application. This code will compute the file size when button is clicked. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
There you have it we successfully created a File Size Converter using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!
Download
We will be using JavaScript as a server-side scripting language because It allows greater control of your web page behavior than HTML alone. It is embedded in HTML that responsible to allow user to interact with the computer .
Getting started:
First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.
The Main Interface
This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
- <!DOCTYPE html>
- <html
lang
=
"en"
>
- <head
>
- <meta
charset
=
"UTF-8"
name
=
"viewport"
content
=
"width=device-width, initial-scale=1"
/
>
- <link
rel
=
"stylesheet"
type
=
"text/css"
href
=
"css/bootstrap.css"
/
>
- </
head
>
- <body
>
- <nav class
=
"navbar navbar-default"
>
- <div
class
=
"container-fluid"
>
- <a
class
=
"navbar-brand"
href
=
"https://sourcecodester.com"
>
Sourcecodester</
a
>
- </
div
>
- </
nav>
- <div
class
=
"col-md-3"
></
div
>
- <div
class
=
"col-md-6 well"
>
- <h3
class
=
"text-primary"
>
JavaScript - File Size Converter</
h3
>
- <hr
style
=
"border-top:1px dotted #ccc;"
/
>
- <div
class
=
"col-md-3"
></
div
>
- <div
class
=
"col-md-6"
>
- <div
class
=
"form-inline"
>
- <input
type
=
"number"
class
=
"form-control"
id
=
"number1"
/
>
- <select
class
=
"form-control"
id
=
"file1"
>
- <option
value
=
"Byte"
>
Byte</
option
>
- <option
value
=
"KB"
>
KB</
option
>
- <option
value
=
"MB"
>
MB</
option
>
- <option
value
=
"GB"
>
GB</
option
>
- </
select
>
- </
div
>
- <br
/
>
- <div
class
=
"form-inline"
>
- <input
type
=
"number"
class
=
"form-control"
id
=
"number2"
disabled
=
"disabled"
/
>
- <select
class
=
"form-control"
id
=
"file2"
>
- <option
value
=
"Byte"
>
Byte</
option
>
- <option
value
=
"KB"
>
KB</
option
>
- <option
value
=
"MB"
>
MB</
option
>
- <option
value
=
"GB"
>
GB</
option
>
- </
select
>
- </
div
>
- <br
/
>
- <center
><button
class
=
"btn btn-primary"
onclick
=
"calculateSize();"
>
Calculate</
button
></
center
>
- </
div
>
- </
div
>
- <script
src
=
"js/script.js"
></
script
>
- </
body
>
- </
html
>
Creating the Script
This code contains the script of the application. This code will compute the file size when button is clicked. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
- function
calculateSize(
)
{
- var
number=
document.getElementById
(
'number1'
)
.value
;
- if
(
number==
""
)
{
- alert(
"Please enter something"
)
;
- }
else
{
- var
result;
- var
file1=
document.getElementById
(
'file1'
)
.value
;
- var
file2=
document.getElementById
(
'file2'
)
.value
;
- if
(
file1==
"Byte"
&&
file2==
"Byte"
)
{
- result=
parseInt(
number)
;
- }
else
if
(
file1==
"Byte"
&&
file2==
"KB"
)
{
- result=
Math
.round
(
parseInt(
number)
/
1024
)
- }
else
if
(
file1==
"Byte"
&&
file2==
"MB"
)
{
- result=
Math
.round
(
parseInt(
number)
/
1024
/
1024
)
;
- }
else
if
(
file1==
"Byte"
&&
file2==
"GB"
)
{
- result=
Math
.round
(
parseInt(
number)
/
1024
/
1024
/
1024
)
;
- }
else
if
(
file1==
"KB"
&&
file2==
"Byte"
)
{
- result=
parseInt(
number)
*
1024
;
- }
else
if
(
file1==
"KB"
&&
file2==
"KB"
)
{
- result=
parseInt(
number)
;
- }
else
if
(
file1==
"KB"
&&
file2==
"MB"
)
{
- result=
Math
.round
(
parseInt(
number)
/
1024
)
;
- }
else
if
(
file1==
"KB"
&&
file2==
"GB"
)
{
- result=
Math
.round
(
parseInt(
number)
/
1024
/
1024
)
;
- }
else
if
(
file1==
"MB"
&&
file2==
"Byte"
)
{
- result=
parseInt(
number)
*
1024
*
1024
;
- }
else
if
(
file1==
"MB"
&&
file2==
"KB"
)
{
- result=
parseInt(
number)
*
1024
;
- }
else
if
(
file1==
"MB"
&&
file2==
"MB"
)
{
- result=
parseInt(
number)
;
- }
else
if
(
file1==
"MB"
&&
file2==
"GB"
)
{
- result=
Math
.round
(
parseInt(
number)
/
1024
)
;
- }
else
if
(
file1==
"GB"
&&
file2==
"Byte"
)
{
- result=
parseInt(
number)
*
1024
*
1024
*
1024
;
- }
else
if
(
file1==
"GB"
&&
file2==
"KB"
)
{
- result=
parseInt(
number)
*
1024
*
1024
;
- }
else
if
(
file1==
"GB"
&&
file2==
"MB"
)
{
- result=
parseInt(
number)
*
1024
;
- }
else
if
(
file1==
"GB"
&&
file2==
"GB"
)
{
- result=
parseInt(
number)
;
- }
- document.getElementById
(
'number2'
)
.value
=
result;
- }
- }
There you have it we successfully created a File Size Converter using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!
Download
You must upgrade your account or reply in the thread to view hidden text.