Professional Documents
Culture Documents
4.1 Javascript Introduction
4.1 Javascript Introduction
4.1 Javascript Introduction
1 JavaScript Introduction
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
JavaScript
• 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)
• JavaScript = ECMA-Script
• Syntax like C
5
Browser Problems
• Before users can run JavaScript scripts, they
may need to change the browser’s security
settings
– By default
• Internet Explorer (IE) disables JavaScript
• FireFox (FF) enables JavaScript
• Browsers render web pages differently
• DOM programming is cumbersome
– each vendor implements DOM differently
• legacy of browser wars
7
JavaScript in IE
<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 JavaScript
• 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 JavaScript source files
– s
9
Example
<html xmlns = "http://www.w3.org/1999/xhtml"><head>
<title>Welcome</title>
<link rel="stylesheet" href="sample.css" />
<script language="JavaScript" src="sample.js"></script>
</head><body>
<script type = "text/javascript">
<!—
var title = "Welcome";
var text = "415 is fun!";
document.writeln("<h1>"+title+"</h1>");
document.writeln("<p>"+text+"</p>");
alert (title+"\n"+text);
//-->
</script>
</body></html>