JavaScript is a scripting language most often used for client-side web development. It was the originating dialect of the ECMAScript standard.
(From Wikipedia JavaScript)
<html>
<head>
<script type="text/javascript">
alert("Hi There!");
</script>
</head>
<body>
Nothing here..
</body>
</html>
It can also be a separate file located on a webserver:
<html> <head> <script type="text/javascript" SRC="ascript.js"></script> </head> <body> Nothing here.. </body> </html>or both:
<html>
<head>
<script type="text/javascript" SRC="ascript.js"></script>
<script type="text/javascript">
alert("Hi There!");
</script>
</head>
<body>
Nothing here..
</body>
</html>
Variables
var avariable; // Declares a variable called avariable avarable = 15; // Assigns 15 to that variableVariables are not generally "typed".. You can do this:
var anothervariable = "My Name Is Shawn"; alert(anothervariable); anothervariable = 15; alert(anothervariable);Comments
// single line comment /* multiline comment */Comparison, Conditionals and Loops
var what = true;
var now = 5;
if (what == now)
{
// Then do this
}
else if (1 != 1)
{
// Or this
}
else
{
// Those weren't true.. Let's loop
for (var i = 0; i < now; i++)
{
alert("Counting: " + i);
}
}
Built-In Functions
function dosomething(somedata)
{
var whattosay = "Hi there " + somedata + " , nice to meet you!";
alert(whattosay);
}
As you can see, the + symbol string concatenation.
dosomething("shawn");
or
<input type="button" name="name" value="blah" onClick="dosomething(this.value);" />
var dosomethingelse = function(somedata) {
var whattosay = "Hi there " + somedata + " , nice to meet you!";
alert(whattosay);
};
<script type="text/javascript">
function changeIt()
{
var thediv = document.getElementById('mydiv');
thediv.innerHTML = "soemthing else";
}
</script>
My Div:
<div id="mydiv">Something</div>
<input type="button" name="changeit" value="Change It" onClick="changeIt();" />
My Div: