Javascript
About JAvascript
-
Javascript is not JAVA !
- interpreted computer programming language
- JavaScript is a prototype-based scripting language that is dynamic
- web browsers
- ECMAScript
Variables
Javascript is a type safe language
var x; //defines the variable x, although no value is assigned to it by default
var y = 2; //defines the variable y and assigns the value of 2 to it
var z = "String";
Functions
- keyword function
function add(a, b) {
return a + b;
}
var c = add(2,3);
objects
"Everything" in JavaScript is an Object: a String, a Number, an Array, a Function etc.
-
Object properties
var text = "javascript is cool";
var a = text["length"];
var b = text.length;
- Define your own objects
var cat = {colour: "grey", name: "Bogdan"};
delete cat.colour;
var colour = cat.colour;
- Adding methods to an object
var rabbit = {};
rabbit.speak = function(line) { print("The rabbit says '", line, "'"); };
rabbit.speak("Well, now you're asking me.");
built-in objects
- String
var txt = "string";
charAt() : Returns the character at the specified index indexOf() : Returns the position of the first found occurrence of a specified value in a string toLowerCase() : Converts a string to lowercase letters toUpperCase() : Converts a string to uppercase letters
- Array
var myCars=["Saab","Volvo","BMW"]; var name=myCars[0];
pop() : Remove the last element of an array
push() : Add new elements to the end of an array - push()
reverse() :Reverse the order of the elements in an array
POO in Javascript
function People(name, age) {
this.name = name;
this.age = age;
this.speak = function() {
return "I am a person";
};
}
var bogdan = new People("Bogdan", 23);
var andrei = new People("Andrei", 18);
bogdan.speak();
Each time a function is called with the new keyword in front of it "this" variable will point at a new object.
JavaScript and HTML
- Placing JavaScript in your pages
<script>
function myFunction() {}
</script>
<script src="myJsFile.js"></script>
browser Object
- represents an open window in a browser.
window.alert("Hello"); Displays an alert box with the message "Hello" and an OK button
window.confirm("Confirm"); Displays a dialog box with a message and an OK and a Cancel button
window.close(); Closes the current window
document object
- each HTML document loaded into a browser
document.write("Hello World!");
var a = "test";
document.write(a);
document.title; Return the title of the document;
- searching for elements in DOM
document.getElementById(id)
- changing HTML Content
document.getElementById(id).innerHTML = new HTML;
var htmlText = document.getElementById(id).innerHTML;
- changing element attribute
document.getElementById(id).attribute = new value;
var attributeValue = document.getElementById(id).attribute;
Html Events
- an event occurs when something happens in a browser window
- html event attributes
-
onclick - Fires on a mouse click on the element onblur - Fires the moment that the element loses focus onchange - Fires the moment when the value of the element is changed onload - Fires after the page is finished loading
<script>
myFunction = function() {
alert("Button clicked");
}
</script>
<button style="width:100px;" onclick=myFunction()> Click me</button>
Javascript
By Bogdan Posa
Javascript
- 1,146