Programming Computer Programming For Beginners Learn The Basics of HTML5 JavaScript CSS 2nd Edtion Coding C Programming Java Programming Web Design JavaScript Python HTML and CSS PDF
Programming Computer Programming For Beginners Learn The Basics of HTML5 JavaScript CSS 2nd Edtion Coding C Programming Java Programming Web Design JavaScript Python HTML and CSS PDF
2nd Edition
© Copyright 2016 - All rights reserved.
In no way is it legal to reproduce, duplicate, or transmit
any part of this document in either electronic means or
in printed format. Recording of this publication is strictly
prohibited and any storage of this document is not
allowed unless with written permission from the
publisher. All rights reserved.
The information provided herein is stated to be truthful
and consistent, in that any liability, in terms of
inattention or otherwise, by any usage or abuse of any
policies, processes, or directions contained within is the
solitary and utter responsibility of the recipient reader.
Under no circumstances will any legal responsibility or
blame be held against the publisher for any reparation,
damages, or monetary loss due to the information
herein, either directly or indirectly.
Respective authors own all copyrights not held by the
publisher.
Legal Notice:
This book is copyright protected. This is only for
personal use. You cannot amend, distribute, sell, use,
quote or paraphrase any part or the content within this
book without the consent of the author or copyright
owner. Legal action will be pursued if this is breached.
Disclaimer Notice:
Please note the information contained within this
document is for educational and entertainment purposes
only. Every attempt has been made to provide accurate,
up to date and reliable, complete information. No
warranties of any kind are expressed or implied. Readers
acknowledge that the author is not engaging in the
rendering of legal, financial, medical or professional
advice.
By reading this document, the reader agrees that under
no circumstances are we responsible for any losses,
direct or indirect, which are incurred as a result of the
use of information contained within this document,
including, but not limited to, —errors, omissions, or
inaccuracies.
Table of Contents
Introduction
Chapter 1: Introduction to Web Development
Chapter 2: Learning HTML5: The Language of the Web
Chapter 3: Understanding CSS: Stylizing the Internet
Chapter 4: Learning JavaScript: Making the Web More
Interactive
Conclusion
Give away
Hi I’m Marco from Mjg Publishing; I wanted to thank
you for supporting this book. I truly hope you find the
information useful as you begin/continue your journey
through programming. A big part of the reason I do this
is getting to give value and help those around me learn
and it means a lot that you’ve helped me towards that
goal.
In return, I’d like to invite you join my list. On this list I’ll
be giving you exclusive access to discounts and
giveaways for all our future books we release as well as a
chance to win a $50 Amazon gift card every month.
h1{ h1 {
font-weight: normal; font: normal 20px/12px
Georgia, Tahoma,
font-size: 20px;
verdana;
line-height: 12px;
}
font-family: georgia,
tahoma, verdana;
}
p{ P{color: 4f39ac;
background: #f4f4f4;
color: #4f39ac;
line-height: 16px;}
background: #f4f4f4;
line-height: 16px;
}
Fundamentals of JavaScript
JavaScript has become one of the most prominent
languages on the internet. It can be used across
platforms and various browsers. Therefore in order to
use this language in practical world, we need to first
learn about its fundamentals. This would give you a
foundational understanding of JavaScript.
1. Variables
As you already know, variables are containers in
which data can be stored. The data stored in a variable
can be an expression or a value. Expressions can be of
three types in JavaScript language.
Expression Description
2. Data types
As you may already know, data types denote the kind
of values that can be used in a programming language.
There are two kinds of data types in Java Script:
a) Primitive
b) Non-primitive
It is dynamic type language; this means that you don’t
need to specify type of variable because it is used
dynamically by JavaScript engine. Here, you need to
use var to specify the data type.
In JavaScript, there is no difference between floating-
point values and integer values. Instead, all numbers
are considered to be floating-point values in
JavaScript.
Primitive Data Types-They are of five types:
(1) String- this type represents the sequence of
characters
(2) Boolean- it represents value as either true or false
(3) Number- it represents the numeric value
(4) Undefined- as the name suggests, it shows the
undefined values
(5) Null- represents no value at all.
Non primitive data types-
(1) Array- which is group of similar values
(2) Object- represents instance by which we can
access members
(3) RegExp- reflects regular expression
Let us look into details of data types:
Arrays- They are almost similar to variables except that
they can hold multiple expressions and values under one
name and thus making them a powerful data type. There
is no limit to the amount of data which can be stored in
arrays as long as it is within the scope. One can access
any value of any item and any given point of time
provided that it is declared in the script. Similar type of
data can be stored under one single array and that
particular array can be assigned a name which is related
to the items it consists of.
Let us look at an example:
var shapes = new Array(“rectangle”, “square”,
“triangle”);
var colors = new Array( “red”, “pink”, “orange”);
To access any item in the array you have to use its ID. It
is the item’s position in the array. Arrays always start
with a 0 as an ID instead of 1 which might get confusing
in the beginning but with practice you can find the
concept easy to learn. The ID’s work in ascending order;
like 0,1,2,3 and so on. Below is an example to understand
this concept-
var colors = new Array( “red”, “pink”, “green”);
document.write( “red: “+colors[0]);
document.write( “pink: “+colors[1]);
document.write( “green: “+colors[2]);
In an array you can also assign values to a particular
position. For example:
var colors = new Array ();
colors [0] = “red”;
colors [1] = “pink”;
colors [2] = “green”;
document.write (“pink: “+colors [2]);
//update pink to blue
Colors [1] = “blue”;
document.write (“blue: “+ colors [2]);
Objects- The objects are written with curly brackets.
The properties of object are written as –:
name: value pairs, separated by commas
Example for objects:
Var person = {first name: “Ryan”, last name: “Booth”,
age:35, eyeColor: “brown”};
The above example has four properties; age, eye color,
first name and last name.
Operators – To perform any operation in JavaScript
we need operators. They include comparison,
subtraction, addition and many more. There are
four basic operators:
Assignment
Arithmetic
Logical
Comparison
Arithmetic operators-These operators perform
basic mathematical operations like division,
addition, multiplication, subtraction and so on.
Operator Description
* Multiplication
- Subtraction
+ Addition
/ division
- Decrement
++ increment
+= Variable is assigned
addition value
-= Variable is assigned
subtraction value
*= Variable is assigned
multiplication value
/= Variable is assigned
division value
%= Variable is assigned
modulo value
Operator Description
== Equal to
!= Not equal to
! Not
&& And
|| Or
3. Conditional Statements
Like other programming languages, there are different
kinds of conditional statements available in
JavaScript. They allow you to define a condition
whose results determine the action that will be done.
They allow your program to make a decision based on
the outcome of the condition and perform the relevant
action.
In order to create any kind of logic in a programming
language, conditional statements act as the basis for
it. Conditional statements can be written in four ways
under JavaScript.-
Statement description
Examples:
if
var num=20;
if (num==10)
{
document.write( “num is equal to 10”);
}
if..else
var num= 20;
if (num==10)
{
Document.write(“num is equal to 10”);
}
Else
{
Document.write( “num is NOT equal to 10,
num is: “+num);
}
if..else if…else
Var num=20;
If (num==10)
{
document.write( “num is equal to 10”);
}
else if (num==20)
{
document.write( “num is equal to 20”);
}
else
{
document.write( “num is: “+num”);
}
( d) switch
var num= 20;
switch (num)
{
case 10:
document.write(“ num is equal to 10”);
break;
case 20:
Document.write(“ num is equal to 20”);
break;
default:
document.write( “num is: “+num);
}
In the above example case clause defines whether the
value of data used is equal to value of switch and the
break statement stops the switch statement from
executing the rest of the statement. The default clause
identifies a default script if none of the case statements
are executed or if break statements are not there in the
executed statements. For example
var num=20;
switch(num)
{
case 10:
document.write(“num is equal to 10”);
Break;
case 20:
document.write(“num is equal to 20”);
default:
document.write(“num is: “+num);
}
Therefore the result of above example would be “num is
equal to 20” followed by “num is:20”
5) Functions
Functions are the containers for the script. They are only
to be executed by a call to the function or an event.
Hence, they are not executed when browser is initially
loaded and executes the script that is the part of web
page.
For structuring a function function word followed by a
space is used. The name of the function can be anything;
however one should ensure that the name given is
related to the task it will perform. Example:
var num =20;
function changevariablevalue()
{
num=21;
}
Changevariablevalue();
Document.write( “num is: “+num);
6) Loops
Loops are used to iterate the arrays and execute script
along while accessing their values. Loop typically
consists of variable that has been assigned a numeric
value. That variable is then used with a comparison
operator to compare it against other value. After this the
numeric value is increased or decreased. The comparison
in the loop typically determines whether the initial value
of a variable is greater or less than another numeric
value. So till the time that condition is true the loop will
run and the value of variable is increased or decreased
till the time condition becomes false.
Looping allows you to execute a single block of code
multiple times till a given condition is met. JavaScript
offers two kinds of loops, the for loop, and the while
loop.
a) The for loop
The for loop allows the same block of code statements
to be repeatedly executed till a certain specified
condition is met. The syntax of the for loop is given as
follows.
30. For (initial expression; condition
expression; loop expression)
31. {Statements}
Example:
var colors = new array( “red”, “pink”, “green”);
for var (i=0; i<colors.length; i++)
{
Document.write( “The color is: “+ colors[i] +<br/>);
}
The above example shows a for loop that runs till the
time numeric value is less than the length of the array.
b) The while loop
With the while loop, it becomes possible to control the
number of times the loop is executed. In other words,
this loop is used when you know the number of times
a loop needs to be executed. As such, you need to
make sure that loop is written properly so that it does
not get repeated infinitely. The syntax of the while
loop is given below.
32. While (expression) {
33. Statement(s) to be executed if
expression is true
34. }
Example:
var i =0;
while (i<10)
{
document.write(i + “<br/>”);
i++:
}
In the above example script in this type of loop
includes a line that repeats the numeric value until the
condition in while loop is false. So if this line is not
there then it will be an endless loop.
The Hello World Example in JavaScript
Here is the hello world example in JavaScript. This will
illustrate how the language works and familiarize you
with the syntax as well. As you will see, the language is
quite simple.
<Html>
<Body>
<script language=“javascript”
type=“text/javascript”>
<!—
document.write (“Hello World!”)
//—>
</script>
</body>
</html>
The Syntax in JavaScript
JavaScript is supposed to be implemented with the help
of JavaScript statements. These statements are placed
inside the <script>… </script> HTML tags in a web
page. The <script> tags can be placed anywhere within
the HTML document. The <script> tag will alert the
browser to interpret the text between the tags as a script.
An example of the JavaScript syntax is given below.
<script language=“javascript”
type=“text/javascript”>
Code in JavaScript
</script>
The script tag has two crucial attributes. They are given
below.
1. Language: This attribute is used to specify which
scripting language is being used. Of course, since
you are using JavaScript, the value of this attribute
is javascript. On the other hand, this attribute is
rarely used these days for the recent versions of
HTML and XHTML.
2. Type: This attribute is now currently being used for
indicating the scripting language. The value of this
attribute should be set as ‘text/javascript.’
Line Breaks and Whitespace
JavaScript will ignore whitespaces. Therefore, you use
newlines, tabs, and spaces in your scripts freely. Of
course, you should be using them to format your
programs and make them look consistent and neat. This
allows the code to be read and understood easily.
Semicolons
Generally, semicolons are used to mark the end of
statements. This is the case in languages like Java, C++
and C. However, JavaScript makes semicolons optional
in one condition. You need to ensure that each statement
has been placed on a separate line. As long as you meet
this condition, you can avoid inserting semicolons in the
code. However, using semicolons is a good practice
especially when you are learning to program.
Case Sensitivity
You need to ensure the proper uses of cases in JavaScript
as this language is case-sensitive. Therefore, all
keywords, function names, variables and other
identifiers must be typed with the correct case.
Consistency is vital. In JavaScript, the identifiers hi and
HI will have different meanings.
How to Place JavaScript in HTML Documents?
One of the good things about JavaScript is that you have
the flexibility to add the code anywhere you want in an
HTML document. Nonetheless, it is an excellent idea to
follow the preferred methods. This can improve the
readability and execution of the JavaScript code in the
HTML document. Here are the best ways to include
JavaScript in HTML.
1. Inside the <head> tags
This option is generally used when you want the script
to run when an event takes place. For example, you
may want the script to run when the user clicks
something somewhere. In these situations, the script
will be placed inside the <head> tags as shown in the
following example.
35. <Html>
36. <Head>
37. <script type=“text/javascript”>
38. <!—
Function sayHello () {
Alert (“Hello World”)
}
//—>
</script>
</head>
<Body>
<input type=“button” onclick=“sayHello ()”
value=“Say Hello” />
</body>
</html>
2. Inside the <body> tags
In some cases, you may need the script to run while
the page loads. This is done to allow the script to
generate the content located in the page. In this case,
the script will have to be placed inside the <body>
tags. In this method, you will not have any function
that has been defined with JavaScript. You can see the
example given below for such an inclusion of
JavaScript.
39. <Html>
40. <Head>
41. </head>
42. <Body>
43. <script type=“text/javascript”>
44. <!—
document.write (“Hello World”)
//—>
</script>
<p>Web Page Body </p>
</body>
</html>
3. Inside the <head> and <body> tags
If you want, you can combine the two methods above.
In other words, you can place your JavaScript code
inside both, the <head> and the <body> tags, in the
same document. Check out the example given below
to see how it works.
45. <Html>
46. <Head>
47. <script type=“text/javascript”>
48. <!—
Function sayHello () {
Alert (“Hello World”)
}
//—>
</script>
</head>
<Body>
<script type=“text/javascript”>
<!—
document.write (“Hello World”)
//—>
</script>
<input type=“button” onclick=“sayHello ()”
value=“Say Hello” />
</body>
</html>
4. Inside an External File
Including JavaScript inside the HTML document is a
good idea when you are creating simple web pages.
However, as you create websites or complex web
pages, it is possible that you can find yourself reusing
the same JavaScript code in multiple locations. You
may even end up using the same code in multiple web
pages.
As you have seen in CSS, it is possible for you to create
an entirely separate file to keep your JavaScript code.
You can put in all the JavaScript code that needs to be
used in multiple locations or web pages inside an
external file. That file should have the ‘.js.’ extension
and be associated with the web pages that require the
code. The following example illustrates how an
external JavaScript file can be included in an HTML
document. In this example, filename.js is the file used
for containing the JavaScript code.
49. <Html>
50. <Head>
51. <script type=“text/javascript”
src=“filename.js” ></script>
52. </head>
53. <Body>
54. …….
55. </body>
56. </html>
Events in JavaScript
An event is an action that causes a JavaScript code to be
executed. Events are generally triggered by the users. For
example, the click of a mouse can be an event. Based on
the events, it is possible to code a response in JavaScript.
For example, displaying a message can be a response to
an event. These responses can be said to be the event
handlers as they handle the event taking place. In
JavaScript, it is possible to classify events in a number of
ways. After all, there are a huge number of them.
15. The onClick event
The most common event type is the onClick event.
This takes place whenever the user left-clicks the web
page. Against this event, you can make the
appropriate response such as displaying a message or
putting your validation. The following example
illustrates the use of such an event for displaying a
message.
57. <Html>
58. <Head>
59. <script type=“text/javascript”>
60. <!—
Function sayHello () {
Alert (“Hello World”)
}
//—>
</script>
</head>
<Body>
<p>Click the following button and see
result</p>
<Form>
<input type=“button” onclick=“sayHello ()”
value=“Say Hello” />
</form>
</body>
</html>
16. onSubmit Event type
This type of event occurs when a form is submitted.
The form validation can be put against this event.
Let us look at an example-
If the validate() function in the below mentioned
example returns true then only the form will get
submitted, if not that than the form will not be
submitted.
<html>
<head>
<script type= “text/javascript”>
<!—
function validation() {
all validation goes here
…….
return either true or false
}
//— >
</script>
</head>
<body>
<form method= “post” action= “t.cgi” onsubmit=
“return validate()”>
…….
<input type “submit” value= “Submit”/>
</form>
</body>
</html>
17. onmouseover and onmouseout
This event gives a nice effect both with images and
text. When you bring your mouse over an element
onmouseover event is triggered and when the
mouse is moved out then onmouseout is triggered.
Example:
<html>
<head>
<script type= “text/javascript”>
<!—
function over () {
document.write ( “mouse over”);
}
function out() {
document.write ( “mouse out”);
}
//— >
</script>
</head>
<body>
<p> drag your mouse inside the division to view
the result:</p>
<div onmouseover= “over ()”
onmouseout=”out()”>
<h2> It is inside the division </h2>
</div>
</body>
</html>
Some tips for using JavaScript
1. Ensure that JavaScript aids the user in reaching a
goal efficiently and quickly. In case it doesn’t that
means that you are probably using it wrong.
2. The JavaScript solutions which you provide to the
user should be naturally better than the earlier
interaction. However, the solution should not be so
different that the user is not able to relate to it with
previous experience. The aim is to ensure that end
user feels satisfied with the solution.
3. In case you are handling sensitive data than do not
rely on JavaScript since all codes are easily available
to read. Apart from that users might turn off
JavaScript in their browsers.
We can say that there are so many possibilities with this
simple but rich scripting language. JavaScript provides
the tools that allow the end users to interact with a web
page after it has been downloaded. In this chapter we
have learnt fundamentals of JavaScript, now let us look
at some frequently asked questions about this
programming language.
FAQ’s
Is JavaScript same as Java?
Ans: No, they are different programming
languages. Java is a general purpose programming
language, whereas JavaScript is used to make
websites more interactive.
What all browsers support JavaScript?
Ans: JavaScript is supported by most of the browsers.
Listing some of them
· Microsoft Internet explorer version 3.0 and
above
· Nestcape Navigator version 2.0 and above
· Safari
· Firefox
· Safari
· Google Chrome
· Opera
How can we add comment in JavaScript?
Ans: Three different types of comments are supported
by JavaScript
a) Multiple line C-Style comments, where everything
written between /*and*/ is a comment
b) Online comments of C++ style. The comments
under this type begin with // and continue till the
next line break.
c) HTML style comments begin with (<!—)
Can a warning be displayed for the user incase
the user’s is unable to execute the
JavaScript code?
Ans: Yes, a warning can be displayed to user about the
JavaScript incapable browsers. The warning text can
be placed between the tags <NOSCRIPT> and
</NOSCRIPT>.
Therefore, the browsers which are JavaScript enabled
will ignore everything between these tags and the
browsers which cannot execute will display your
message on the page. The message will be displayed to
users who have disabled JavaScript and to those as
well whose browsers do not support JavaSacript.
Is it possible to delete a JavaScript?
Ans: You cannot always delete JavaScript variable, it
depends on the following conditions:
a) JavaScript variable cannot be deleted if it has
been declared with keyword var x; at the first time.
However, as a workaround, variable can be set to
null or to undefined. But this is not equal to
deleting the variable.
b) In case the variable x appeared first in the script
without any declaration, then the delete operator,
i.e. delete x can be used.
What all cannot be done with JavaScript
programs?
Ans: The following cannot be done with JavaScript
code:
a) Printers or other devices cannot be used on client
side LAN or the user’s system.
b) Accessing files directly on the user’s system or
client side LAN. The only exception is the cookie
files.
c) Implementing of multithreading or
multiprocessing is also not possible.
What is method to delete an array element?
Ans: There are two different ways to delete an array
element in JavaScript, they are:
a) myArray.splice(n,1). This is a slower method but
does not leave a gap in the array.
b) delete myArray[n] . This method is faster but it
leaves a gap in index n.
Is there a way to find out that whether the user
clicked right or left mouse button? Can
onclick event handler be used?
Ans: For the left mouse button the click event is fired.
However, depending upon the browser click may or
may not occur in case of right mouse button.
Especially in Microsoft Internet Explorer click event
doesn’t work for right mouse button. Therefore,
onclick event handler is not a reliable method to do
right vs left mouse button test. Instead we should use
the mousedown events. Most of the browsers support
mouseup and mousedown events for any mouse
button.
What kind of number formatting can be used in
JavaScript?
Ans: Mathematical constants and number literals can
be written is many ways under JavaScript code, such
as:
a) Decimal numbers in exponential form, e.g.: 5.57e-
11
b) Conventional decimal numbers, e.g. : 5, 7, 137 , 1.3
etc.
c) Octal numbers, e.g. : -055 01234 0212
d) Positive octal numbers that begin with zero and
negative octal numbers that start with –zero.
e) Hexadecimals e.g.: 0xFF , -0xCFFF. The positive
hexadecimals begin with 0x and negative
hexadecimals start with -0x .
f) Predefined mathematical constants, e.g. :
Math.PI //pi = 3.14159…
How can we test whether a particular variable
holds a string or a number?
Ans: To test whether a variable holds a string or
number is by using the typeof operator. In case the
variable holds a string , typeof(variable) will return
“string” and if it holds number than it will return
“number”.
In this chapter, we have covered the basic fundamentals
of JavaScript which would have given you a fair idea on
its working. By adding JavaScript to your HTML pages
you can make your website more dynamic and
interactive. We also learned about some important
concepts of this scripting language such as operators,
functions and variables and covered some standard
programming concepts like if statements, switch
statements and loops. However, it is a vast language and
it offers loads of features and functions which can be
learned and brought into practice with time.
How HTML5, CSS and JavaScript work together?
HTML5 itself is a very powerful programming language
but its actual power is realized when it is used with
JavaScript and CSS3. JavaScript has undoubtedly
emerged as one of the best scripting language to render
great visual effects to a website. Graphic libraries like
WebGl in JavaScript allows developers to create
interactive 3D graphics within the browser by utilizing
the plugin free character of HTML canvas element.
All three languages are intertwined in the sense HTML
provides the basic structure of web sites, which are
enhanced and modified by JavaScript and CSS. CSS tells
HTML what colors, fonts and other styles to be used and
JavaScript tells HTML how to perform certain tasks like
making a pop up window etc.
The two most important things to keep in mind while
learning web development are:
a) Use correct HTML tags – The structure of the
content is described by different HTML tags and
each tag has its own meaning and a defined place.
b) Keep your JavaScript, HTML and CSS code
separate- Always put JavaScript and CSS in a
separate file other than the HTML one. This will
make your work simple and you will be able to
manage better. So by keeping all your styles and
functional codes in one place will make things easy.
CONCLUSION
We have now come to the end of this book on web
programming with HTML, CSS, and JavaScript. All of
these programming languages are quite vast in their own
right. However, since they are simple and easy, you
should not have too much of a problem in mastering
them. With hard work and a bit of time, you will be able
to create full-fledged websites in no time at all.
Of course, this book should have given you enough to
have a good base on all three languages. You should have
already seen the hello world examples and noted how
easy they are. As such, you should have no problems with
the basic syntax for each language.
You will have come across the basic functions,
statements, and tags that are commonly used in these
languages. More importantly, you should no longer have
any problems in using them in a web page of your
creation.
In HTML, you came to know how a simple web page can
be written and filled with content. Then, with CSS, you
will have been able to stylize that web page and make it
look attractive. Finally, JavaScript allowed you to make
your web page more interactive. As such, your web page
should now be looking feeling a lot like the web pages
you have previously admired over the internet.
With the end of this book, I wish you all the best for your
future in programming. It is an exciting activity and one
that will be useful for your career in the long run.
Want to Learn more about
Programming?
Check out the other books by Joseph Connor:
Newest release (Oct, 2016): Programming:
Computer Programming For Beginners: Learn The
Basics Of HTML5, JavaScript, & CSS or click the
link https://www.amazon.com/dp/B01LYZGZKN
Python: The Definitive Guide to Learning Python
Programming for Beginners or click the link
https://www.amazon.com/dp/B013NLBA9C
Raspberry Pi 2: The Definitive Beginner’s Guide to
Get Started with Raspberry Projects or click the
linkhttps://www.amazon.com/dp/B013NKMD2Q
Hacking: Hacking for Beginners - Computer Virus,
Cracking, Malware, IT Security or click the link
https://www.amazon.com/dp/B010K7BVOQ
The Amazon Bestseller:Programming:
Computer Programming for Beginners: Learn the
Basics of Java, SQL & C++ - 3. Edition or click the
link https://www.amazon.com/Programming-
Computer-Beginners-Basics-JavaScript-
ebook/dp/B014361TOM
Check out our Facebook and Instagram to receive
updates on the newest releases!