Skip to main content

Home/ Learning Library/ Group items tagged javascript

Rss Feed Group items tagged

Sunny Jackson

JavaScript How To - 0 views

  • The HTML <script> tag is used to insert a JavaScript into an HTML page.
  • <script type="text/javascript"> document.write("Hello World!"); </script>
  • how to add HTML tags to the JavaScript: Example <html> <body> <script type="text/javascript"> document.write("<h1>Hello World!</h1>"); </script> </body> </html>
  • ...4 more annotations...
  • the <script type="text/javascript"> and </script> tells where the JavaScript starts and ends
  • The document.write command is a standard JavaScript command for writing output to a page.
  • By entering the document.write command between the <script> and </script> tags, the browser will recognize it as a JavaScript command and execute the code line.
  • Browsers that do not support JavaScript, will display JavaScript as page content. To prevent them from doing this, and as a part of the JavaScript standard, the HTML comment tag should be used to "hide" the JavaScript. Just add an HTML comment tag <!-- before the first JavaScript statement, and a --> (end of comment) after the last JavaScript statement
Sunny Jackson

JavaScript Where To - 0 views

  • JavaScripts in a page will be executed immediately while the page loads into the browser.
  • Scripts to be executed when they are called, or when an event is triggered, are placed in functions.
  • put all your functions in the head section, this way they are all in one place and do not interfere with page content.
  • ...8 more annotations...
  • <head> <script type="text/javascript"> function message() { alert("This alert box was called with the onload event"); } </script> </head>
  • Scripts placed in the body section are often used to display page content while the page loads.
  • JavaScript can also be placed in external files.
  • External JavaScript files often contains code to be used on several different web pages.
  • External JavaScript files have the file extension .js
  • External script cannot contain the <script></script> tags!
  • To use an external script, point to the .js file in the "src" attribute of the <script> tag:
  • <script type="text/javascript" src="xxx.js"></script>
Sunny Jackson

JavaScript Statements - 0 views

  • JavaScript is Case Sensitive
  • A JavaScript statement is a command to a browser. The purpose of the command is to tell the browser what to do.
  • It is normal to add a semicolon at the end of each executable statement.
  • ...6 more annotations...
  • Using semicolons makes it possible to write multiple statements on one line.
  • JavaScript code (or just JavaScript) is a sequence of JavaScript statements.
  • Each statement is executed by the browser in the sequence they are written.
  • JavaScript statements can be grouped together in blocks. Blocks start with a left curly bracket {, and ends with a right curly bracket }.
  • The purpose of a block is to make the sequence of statements execute together.
  • Normally a block is used to group statements together in a function or in a condition (where a group of statements should be executed if a condition is met).
Sunny Jackson

JavaScript Comments - 0 views

  • Comments can be added to explain the JavaScript, or to make the code more readable. Single line comments start with //.
  • Multi line comments start with /* and end with */.
  • In the following example the comment is used to prevent the execution of a single code line (can be suitable for debugging): Example <script type="text/javascript"> //document.write("<h1>This is a heading</h1>");
  • ...2 more annotations...
  • In the following example the comment is used to prevent the execution of a code block (can be suitable for debugging): Example <script type="text/javascript"> /* document.write("<h1>This is a heading</h1>"); document.write("<p>This is a paragraph.</p>"); document.write("<p>This is another paragraph.</p>"); */
  • In the following example the comment is placed at the end of a code line: Example <script type="text/javascript"> document.write("Hello"); // Write "Hello" document.write(" Dolly!"); // Write " Dolly!"
Sunny Jackson

JavaScript Popup Boxes - 0 views

  • 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.
  • Syntax alert("sometext");
  • ...5 more annotations...
  • <script type="text/javascript"> function show_alert() { alert("I am an alert box!"); } </script>
  • 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. Syntax confirm("sometext");
  • <script type="text/javascript"> function show_confirm() { var r=confirm("Press a button"); if (r==true)   {   alert("You pressed OK!");   } else   {   alert("You pressed Cancel!");   } } </script>
  • 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. Syntax prompt("sometext","defaultvalue");
  • <script type="text/javascript"> function show_prompt() { var name=prompt("Please enter your name","Harry Potter"); if (name!=null && name!="")   {   document.write("Hello " + name + "! How are you today?");   } } </script>
Sunny Jackson

NCCU Library Catalog: A balanced introduction to computer science - 0 views

  • JavaScript and dynamic web pages
  • Algorithms and programming languages
  • JavaScript and user interaction
  • ...10 more annotations...
  • Functions and randomness
  • Abstraction and libraries
  • Conditional execution
  • enabling students to learn programming fundamentals by developing their own interactive Web pages with HTML and JavaScript
  • Conditional repetition
  • Data representation
  • JavaScript strings
  • von Neumann architecture
  • JavaScript arrays
  • transistors and integrated circuits
Sunny Jackson

JavaScript Variables - 0 views

  • Variables are "containers" for storing information.
  • variables can be used to hold values (x=5) or expressions (z=x+y).
  • A variable can have a short name, like x, or a more descriptive name, like carname.
  • ...8 more annotations...
  • 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
  • Creating variables in JavaScript is most often referred to as "declaring" variables.
  • You can declare JavaScript variables with the var keyword: var x; var carname;
  • they have no values yet
  • you can also assign values to the variables when you declare them: var x=5; var carname="Volvo";
  • When you assign a text value to a variable, use quotes around the value.
  • If you assign values to variables that have not yet been declared, the variables will automatically be declared.
  • These statements: x=5; carname="Volvo"; have the same effect as: var x=5; var carname="Volvo";
Sunny Jackson

JavaScript Special Characters - 0 views

  • In JavaScript you can add special characters to a text string by using the backslash sign.
  • Insert Special Characters The backslash (\) is used to insert apostrophes, new lines, quotes, and other special characters into a text string.
  • In JavaScript, a string is started and stopped with either single or double quotes.
  • ...4 more annotations...
  • place a backslash (\) before each double quote
  • This turns each double quote into a string literal:
  • JavaScript will now output the proper text string
  • Code Outputs \' single quote \" double quote \\ backslash \n new line \r carriage return \t tab \b backspace \f form feed
Sunny Jackson

JavaScript Functions - 0 views

  • A function will be executed by an event or by a call to the function.
  • To keep the browser from executing a script when the page loads, you can put your script into a function.
  • A function contains code that will be executed by an event or by a call to the function.
  • ...13 more annotations...
  • You may call a function from anywhere within a page (or even from other pages if the function is embedded in an external .js file).
  • Functions can be defined both in the <head> and in the <body> section of a document. However, to assure that a function is read/loaded by the browser before it is called, it could be wise to put functions in the <head> section.
  • How to Define a Function Syntax function functionname(var1,var2,...,varX) { some code }
  • The { and the } defines the start and end of the function.
  • The parameters var1, var2, etc. are variables or values passed into the function.
  • A function with no parameters must include the parentheses () after the function name.
  • The word function must be written in lowercase letters, otherwise a JavaScript error occurs!
  • Also note that you must call a function with the exact same capitals as in the function name.
  • The return Statement 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.
  • <script type="text/javascript"> function product(a,b) { return a*b; } </script>
  • If you declare a variable, using "var", within a function, the variable can only be accessed within that function. When you exit the function, the variable is destroyed. These variables are called local variables.
  • You can have local variables with the same name in different functions, because each is recognized only by the function in which it is declared.
  • If you declare a variable outside a function, all the functions on your page can access it. The lifetime of these variables starts when they are declared, and ends when the page is closed.
Sunny Jackson

JavaScript Break and Continue Statements - 0 views

  • The break Statement The break statement will break the loop and continue executing the code that follows after the loop (if any).
  • <script type="text/javascript"> var i=0; for (i=0;i<=10;i++)   {   if (i==3)     {     break;     }   document.write("The number is " + i);   document.write("<br />");   } </script>
  • The continue Statement The continue statement will break the current loop and continue with the next value.
  • ...1 more annotation...
  • <script type="text/javascript"> var i=0 for (i=0;i<=10;i++)   {   if (i==3)     {     continue;     }   document.write("The number is " + i);   document.write("<br />");   } </script>
Sunny Jackson

JavaScript Events - 0 views

  • 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.
  • Examples of events: A mouse click A web page or an image loading Mousing over a hot spot on the web page Selecting an input field in an HTML form Submitting an HTML form A keystroke
  • ...5 more annotations...
  • Events are normally used in combination with functions, and the function will not be executed before the event occurs!
  • The onLoad and onUnload events are triggered when the user enters or leaves the page.
  • The onFocus, onBlur and onChange events are often used in combination with validation of form fields
  • onSubmit The onSubmit event is used to validate ALL form fields before submitting it.
  • onMouseOver and onMouseOut are often used to create "animated" buttons.
Sunny Jackson

JavaScript Introduction - 0 views

  • A scripting language is a lightweight programming language
  • Java and JavaScript are two completely different languages in both concept and design!
Sunny Jackson

JavaScript Comparison and Logical Operators - 0 views

  • Comparison operators can be used in conditional statements to compare values and take action depending on the result: if (age<18) document.write("Too young");
  • JavaScript also contains a conditional operator that assigns a value to a variable based on some condition. Syntax variablename=(condition)?value1:value2 
Sunny Jackson

JavaScript If...Else Statements - 0 views

  • Conditional statements are used to perform different actions based on different conditions.
  • 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
  • ...8 more annotations...
  • 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
  • Use the if statement to execute some code only if a specified condition is true. Syntax if (condition)   {   code to be executed if condition is true   }
  • Note that if is written in lowercase letters. Using uppercase letters (IF) will generate a JavaScript error!
  • Notice that there is no ..else.. in this syntax. You tell the browser to execute some code only if the specified condition is true.
  • If...else Statement Use the if....else statement to execute some code if a condition is true and another code if the condition is not true.
  • Syntax if (condition)   {   code to be executed if condition is true   } else   {   code to be executed if condition is not true   }
  • If...else if...else Statement Use the if....else if...else statement to select one of several blocks of code to be executed. Syntax if (condition1)   {   code to be executed if condition1 is true   } else if (condition2)   {   code to be executed if condition2 is true   } else   {   code to be executed if condition1 and condition2 are not true   }
Sunny Jackson

JavaScript Switch Statement - 0 views

  • The JavaScript Switch Statement Use the switch statement to select one of many blocks of code to be executed. Syntax switch(n) { case 1:   execute code block 1   break; case 2:   execute code block 2   break; default:   code to be executed if n is different from case 1 and 2 }
  • Use break to prevent the code from running into the next case automatically.
Sunny Jackson

JavaScript for Loop - 0 views

  • Loops execute a block of code a specified number of times, or while a specified condition is true.
  • 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
  • The for Loop The for loop is used when you know in advance how many times the script should run. Syntax for (variable=startvalue;variable<=endvalue;variable=variable+increment) { code to be executed }
Sunny Jackson

JavaScript For...In Statement - 0 views

  • JavaScript For...In Statement The for...in statement loops through the elements of an array or through the properties of an object. Syntax for (variable in object)   {   code to be executed   }
  • The code in the body of the for...in loop is executed once for each element/property.
Sunny Jackson

JavaScript Try...Catch Statement - 0 views

  • The try...catch statement allows you to test a block of code for errors.
  • 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. Syntax try   {   //Run some code here   } catch(err)   {   //Handle errors here   } Note that try...catch is written in lowercase letters. Using uppercase letters will generate a JavaScript error!
  • The throw Statement The throw statement can be used together with the try...catch statement, to create an exception for the error
Sunny Jackson

JavaScript Throw Statement - 0 views

  • The Throw Statement The throw statement allows you to create an exception. If you use this statement together with the try...catch statement, you can control program flow and generate accurate error messages. Syntax throw exception
  • throw is written in lowercase letters. Using uppercase letters will generate a JavaScript error!
Sunny Jackson

JavaScript Guidelines - 0 views

  • JavaScript is case sensitive - therefore watch your capitalization closely when you create or call variables, objects and functions.
1 - 20 of 27 Next ›
Showing 20 items per page