JS 3 z Java Script 1 html head

  • Slides: 18
Download presentation
JS 3 z Java. Script 1

JS 3 z Java. Script 1

<html> <head> <title>Sample Java Script Page</title> <script type="text/javascript"> function disp_prompt() { var name=prompt("Please enter

<html> <head> <title>Sample Java Script Page</title> <script type="text/javascript"> function disp_prompt() { var name=prompt("Please enter your name", "") if (name!=null && name!="") { document. write("Hello " + name) } } </script> </head> <body> <script type="text/javascript"> disp_prompt() </script> Thanks for visiting my webpage. </body> Functions • Here the script to prompt for the name has been defined as a function in the head • It runs when it is called from the body when the page loads • Java. Script functions can take parameters • Java. Script does do subroutines per se so they must be coded as functions with side effects 2

Functions function smallest (a, b, c) { if (a <= b && a<= c)

Functions function smallest (a, b, c) { if (a <= b && a<= c) thesmallest = a; else { if ( b <= c ) thesmallest = b; else thesmallest = c; } return thesmallest; } No need to define types Could also have used else if structure 3

Arrays 0 1 2 3 4 5 6 25 16 92 1687 13 65

Arrays 0 1 2 3 4 5 6 25 16 92 1687 13 65 11 • Java. Script supports 1 dimensional arrays • Array index starts at 0 not 1 • Arrays can hold anything • Every item in the array should be the same type 4

Arrays 0 1 2 3 4 5 6 25 16 92 1687 13 65

Arrays 0 1 2 3 4 5 6 25 16 92 1687 13 65 11 0 not 1 var my. Numbers new Array (25, 16, 92, 1687, 13, 65, 11) x = my. Numbers [2] + my. Numbers [6] 5

For In Loop • Java. Script uses a special For loops for working with

For In Loop • Java. Script uses a special For loops for working with arrays for (var x in my. Numbers) { document. write(my. Numbers[x] + " ") } 6

Array support • my. Numbers. length returns the number of elements in an array.

Array support • my. Numbers. length returns the number of elements in an array. This is not the same as the number of the last one! • my. Numbers. join( ) returns one string with all of the elements of the array, separated by commas. A different separator can be passed as a string parameter. Very useful for debugging Java. Script code 7

Array Support • my. Numbers. reverse() reverses the order of the elements in the

Array Support • my. Numbers. reverse() reverses the order of the elements in the array. This changes the array • my. Numbers. sort ( ) sorts the array. 8

Slice 0 1 2 3 4 5 6 25 16 92 1687 13 65

Slice 0 1 2 3 4 5 6 25 16 92 1687 13 65 11 • my. Numbers. slice (start, stop) returns a sub-array with the first element at index start and the last at stop -1 • my. Numbers. slice (2, 5) 9

Splice 0 25 1 16 2 92 2 65 3 1687 3 11 4

Splice 0 25 1 16 2 92 2 65 3 1687 3 11 4 13 5 65 6 11 • my. Numbers. splice (start, n) alters an array by starting with element start and removing n elements. Splice alters the array • my. Numbers. splice (2, 3) 10

Sloppy Java. Script • Slice and splice are good illustrations of how sloppy a

Sloppy Java. Script • Slice and splice are good illustrations of how sloppy a language Java. Script is. • my. Numbers. slice (2, 5) is a function that evaluates to a sub array • my. Numbers. splice (2, 3) is a procedure that alters the array • The failure to distinguish between functions and procedures can lead to sloppy thinking and sloppy code. • So be careful! 11

Stack Arrays - Push & Pop • Arrays can be accessed using Push and

Stack Arrays - Push & Pop • Arrays can be accessed using Push and Pop • my. Numbers. push (25) inserts 25 at the end of the array • x = my. Numbers. pop( ) removes the last element form the array and assigns it to x 12

DIY Objects • Java. Script allows for user defined objects and methods • Maybe

DIY Objects • Java. Script allows for user defined objects and methods • Maybe we’ll come back to this if we have time 13

Cookies • Cookies are small packets of information left by a web server with

Cookies • Cookies are small packets of information left by a web server with the user’s browser. • If the browser returns to the server in the future, the server can read the information in the cookie • Cookies can be used to recognize previous visitors • Cookies can also be used as a variable store to pass information from one page to another. 14

Creating a Cookie • document. cookie ="name=Martin"; • document. cookie="phone=0879991234"; • Cookies get stored

Creating a Cookie • document. cookie ="name=Martin"; • document. cookie="phone=0879991234"; • Cookies get stored internally as a string delimited with a semi-colon • Cookie contents must not contain semicolons, commas, nor spaces [If you need them use escape / unescape functions] 15

Reading Cookies • str. Cookie = document. cookie; • str. Cookie will hold all

Reading Cookies • str. Cookie = document. cookie; • str. Cookie will hold all the cookies available to the page • Need to parse this string to extract the data int. Location = str. Cookie. index. Of("phone="); int. Start = int. Location + 6; int. End = str. Cookie. index. Of("; ", int. Start); str. Ph = str. Cookie. substring(int. Start, int. End); 16

Cookie Access Cookie Time. Out 17

Cookie Access Cookie Time. Out 17

Cookie Time-Out July the. Date = new Date(2007, 6, 20); the. Date. to. UTCstring();

Cookie Time-Out July the. Date = new Date(2007, 6, 20); the. Date. to. UTCstring(); converts to UTC format document. cookie = "name=Martin; expires=" + the. Date; • The default lifespan of a cookie is the current browser session • This lifespan can be extended by the expires attribute • It is usual to set the expiry to be x days from now. This requires some calculations 18