JavaScript Code Examples: Part One

JavaScript - one of the programming languages in the client-side, an useful programming language for web development. The syntax of JavaScript look like Java but it's easier to learn. And this post is the first part in the series of JavaScript code examples, simple & easy to master.


Sampled by © JavaScriptBank.com
JavaScript is a scripting language used to enable programmatic access to objects within other applications. It is primarily used in the form of client-side JavaScript for the development of dynamic websites. JavaScript is a dialect of the ECMAScript standard and is characterized as a dynamic, weakly typed, prototype-based language with first-class functions. JavaScript was influenced by many languages and was designed to look like Java, but to be easier for non-programmers to work with. [via  WikiPedia ]

Simple alert box:


<html>
<head>
<script type="text/javascript">
function show_alert()
{
 alert("Simple alert box!");
}
</script>
</head>
<body>

<input type="button" onclick="show_alert()" value="Show alert" />

</body>
</html>

Alert box with line breaks:


<html>
<head>
<script type="text/javascript">
function show_alert()
{
 alert("This is how simple it is to" + 'n' + "add line breaks to an alert box!");
}

</script>
</head>
<body>

<input type="button" onclick="show_alert()" value="Show alert" />

</body>
</html>

Display a confirmation dialog:


<html>
<head>
<script type="text/javascript">

function show_confirm()
{
 var val = confirm("Press a button");
 if (val == true)
 {
 alert("You pressed OK!");
 }
 else
 {
 alert("You pressed Cancel!");
 }
}
</script>
</head>
<body>

<input type="button" onclick="show_confirm()" value="Show a confirm box" />

</body>
</html>

Simple timer:


<html>
<head>
<script type="text/javascript">

function fireTimer()
{
 var t = setTimeout("alert('2 seconds!')",2000);
}
</script>
</head>

<body>

<input type="button" value="Display timer" onClick = " fireTimer()">

</body>
</html>

Determine which mouse button was clicked:


<html>
<head>
<script type="text/javascript">

function whichButtonClick(event)
{
 if (event.button==2)
 {
 alert("You clicked the right mouse button!");
 }
 else
 {
 alert("You clicked the left mouse button!");
 }
}
</script>
</head>

<body onmousedown="whichButtonClick(event)">

</body>

</html>

Reset a form:


<html>
<head>
<script type="text/javascript">
function formReset()
{
 document.getElementById("testform").reset();
}

</script>
</head>

<body>

<form id="testform">
Name: <input type="text" size="20"><br />

Address: <input type="text" size="20"><br />
<br />
<input type="button" onclick="formReset()" value="Reset">

</form>

</body>
</html>

Language
Translate this page to English Translate this page to French Translate this page to Vietnamese

Recent articles
How to open a car sharing service
Vue developer as a vital part of every software team
Vue.js developers: hire them, use them and get ahead of the competition
3 Reasons Why Java is so Popular
Migrate to Angular: why and how you should do it
The Possible Working Methods of Python Ideology
JavaScript Research Paper: 6 Writing Tips to Craft a Masterpiece
Learning How to Make Use of New Marketing Trends
5 Important Elements of an E-commerce Website
How To Create A Successful Prototype For Your PCB


Top view articles
Top 10 Beautiful Christmas Countdown Timers
Adding JavaScript to WordPress Effectively with JavaScript Localization feature
65 Free JavaScript Photo Gallery Solutions
16 Free Code Syntax Highlighters by Javascript For Better Programming
Best Free Linux Web Programming Editors
Top 10 Best JavaScript eBooks that Beginners should Learn
Top 50 Most Addictive and Popular Facebook mini games
More 30 Excellent JavaScript/AJAX based Photo Galleries to Boost your Sites
Top 10 Free Web Chat box Plug-ins and Add-ons
The Ultimate JavaScript Tutorial in Web Design


Free JavaScript Tutorials & Articles
at www.JavaScriptBank.com