Java Script cont 2 Scripting Languages Distinguishing features










- Slides: 10

Java. Script (cont. )

2 Scripting Languages • Distinguishing features – not compiled – interpreted – not strongly typed – subprograms can be added at run time – text of program code can be directly executed • programs can be constructed at run time

3 Scripting Languages (cont. ) • Typical additional features – associative arrays – regular expressions – good string processing – objects, classes, inheritance • functionality can be added at run time

4 Java. Script • Scripting language – associative arrays – DOM (Document Object Model) is directly accessible • Adds functionality to web pages • Runs on the client side – interpreter is embedded in the browser • Makes it possible to change XHTML elements and their properties – Dynamic HTML (DHTML) • Java. Script = ECMA-Script • Syntax like C

5 Java. Script vs. Java • Java. Script is quite different from Java – similar syntax • because they both descend from C – Java is strongly typed – Java is compiled (into byte code) • actually hybrid because byte code is mostly interpreted – Java has strong Object Oriented foundation • in Java. Script, OO was an afterthought • now is getting better • The name was primarily a marketing decision – Sun and Netscape against Microsoft • We will primarily learn Java. Script in terms of its differences to Java – We assume that you know Java well!

6 Browser Problems • Before users can run Java. Script scripts, they may need to change the browser’s security settings – Some browsers • disable Java. Script, e. g. , Internet Explorer 7 (IE) • enable Java. Script, e. g. Fire. Fox (FF) • Browsers render web pages differently • DOM programming is cumbersome – each vendor implements DOM differently • legacy of browser wars

7 <script> Element • <script> element – either contains scripts • an entire program can be placed there – or contains reference to external scripts file • this is the preferred way! • type attribute – – specifies the scripting language used text/javascript for Java. Script • there is also VBScript language – Microsoft's effort to dominate the market • src attribute – – URL of a file with an external script standard file extension. js for Java. Script source files

8 Java. Script in a Web Page • Typically, <script> is placed within the <head> element of the XHTML document – the contents inside <head> is interpreted first – this is the preferred way • But <script> elements can be placed in the <body> element, too – they are executed in the order in which they appear in the XHTML document • Script fragments occur in the attributes of other elements – e. g. in elements of a form (<input> button), <div> element – to define the response to events – typically just a function call

9 Writing a Web Page • The DOM document object – represents the XHTML document of the web page currently displayed in the browser • document. write() or document. writeln() – insert XHTML code into the XHTML document – once the page is rendered, document. write() replaces the entire page – this allows us to specify (portions of) a web page via scripting! • alert() – displays a dialog with message passed a the argument string • prompt() – displays a dialog with message passed a the argument string – and an input field where the user can type a text

10 Example <html xmlns = "http: //www. w 3. org/1999/xhtml"><head> <title>Welcome</title> <link rel="stylesheet" href="sample. css" /> <script language="Java. Script" src="sample. js"></script> </head><body> <script type = "text/javascript"> var title = "Welcome"; var text = "315 is fun!"; document. writeln("<h 1>"+title+"</h 1>"); document. writeln("<p>"+text+"</p>"); alert (title+"n"+text); </script> </body></html>