Resume ke-9 Desain Web

Client Side Programming (Javascript)

Scripting Variable

Rules for JavaScript variable names:
Variable names are case sensitive (y and Y are two different variables)
Variable names must begin with a letter or the underscore character
Note: Because JavaScript is case-sensitive, variable names are case-sensitive.

Examples declaring variable in Javascript :

<html>
    <body>
        <script type="text/javascript">
        <!--
        var a = "Kelas Desain dan Pemrograman Web";
        //-->
        </script>
    </body>
</html>

Scripting Arithmetic
Arithmetic operators are used to perform arithmetic between variables and/or values.
Given that y=5, the table below explains the arithmetic operators: 




Scripting Comparison

Comparison operators are used in logical statements to determine equality or difference between variables or values.
Given that x=5, the table below explains the comparison operators:


Logical Operator :
Logical operators are used to determine the logic between variables or values.
Given that x=6 and y=3, the table below explains the logical operators:




Scripting Branching


Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this.
In JavaScript we have the following conditional statements:

if statement - use this statement to execute some code only if a specified condition is true

if...else statement - use this statement to execute some code if the condition is true and another code if the condition is false

if...else if....else statement - use this statement to select one of many blocks of code to be executed

switch statement - use this statement to select one of many blocks of code to be executed


Scripting PopUp

JavaScript has three kind of popup boxes: Alert box, Confirm box, and Prompt box.

Alert Box
An alert box is often used if you want to make sure information comes through to the user.
When an alert box pops up, the user will have to click "OK" to proceed.

Confirm Box
A confirm box is often used if you want the user to verify or accept something.
When a confirm box pops up, the user will have to click either "OK" or "Cancel" to proceed.
If the user clicks "OK", the box returns true. If the user clicks "Cancel", the box returns false.

Prompt Box
A prompt box is often used if you want the user to input a value before entering a page.
When a prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed after entering an input value.
If the user clicks "OK" the box returns the input value. If the user clicks "Cancel" the box returns null.
 

Scripting Function

The return statement is used to specify the value that is returned from the function.
So, functions that are going to return a value must use the return statement.

<html>
<head>
<script type="text/javascript">
    function product(a,b){
        return a*b;
    }
</script>
</head>
<body>
<script type="text/javascript">
    document.write(product(4,3));
</script>
</body>
</html>
 

Scripting Loops
In JavaScript, there are two different kind of loops:
for - loops through a block of code a specified number of times
while - loops through a block of code while a specified condition is true

Sintax For
<html>
<body>
    <script type="text/javascript">
        var i=0;
        for (i=0;i<=5;i++){
            document.write("The number is " + i);
            document.write("<br />");
        }
    </script>
</body>
</html>
 

Scripting Event
By using JavaScript, we have the ability to create dynamic web pages. Events are actions that can be detected by JavaScript.

Every element on a web page has certain events which can trigger a JavaScript. For example, we can use the onClick event of a button element to indicate that a function will run when a user clicks on the button. We define the events in the HTML tags.


Scripting Error Handling
When browsing Web pages on the internet, we all have seen a JavaScript alert box telling us there is a runtime error and asking "Do you wish to debug?". Error message like this may be useful for developers but not for users. When users see errors, they often leave the Web page.

The try...catch Statement
The try...catch statement allows you to test a block of code for errors. The try block contains the code to be run, and the catch block contains the code to be executed if an error occurs.

Scripting Special Characters
In JavaScript you can add special characters to a text string by using the backslash sign.

0 comments:

Post a Comment

Chit-Chat (Live)