Web Programming Step by Step
Lecture 13
Introduction to JavaScript
Reading: 7.1 - 7.4
Except where otherwise noted, the contents of this presentation are Copyright 2009 Marty Stepp and Jessica Miller.
client-side script: code runs in browser after page is sent back from server
often this code manipulates the page or responds to user actions
PHP already allows us to create dynamic web pages. Why also use client-side scripting?
client-side scripting (JavaScript) benefits:
usability: can modify a page without having to post back to the server (faster UI)
efficiency: can make small, quick changes to page without waiting for server
event-driven: can respond to user actions like clicks and key presses server-side programming (PHP) benefits:
security: has access to server's private data; client can't see source code
compatibility: not subject to browser compatibility issues
power: can write files, open connections to servers, connect to databases, ...
a lightweight programming language ("scripting language")
used to make web pages interactive
insert dynamic text into HTML (ex: user name)
react to events (ex: page load user click)
get information about a user's computer (ex: browser type)
perform calculations on user's computer (ex: form validation)
a we
b standard (but not supported identically by all browsers)
NOT related to Java other than by name and some syntactic similarities
+ =JavaScript interpreted , not compiled
more relaxed syntax and rules fewer and "looser" data types variables don't need to be declared errors often silent (few exceptions)key construct is the function rather than the class "first-class" functions are used in many situations
contained within a web page and integrates with its HTML/CSS content JS <3 similarities:both are interpreted , not compiled both are relaxed about syntax, rules, and types both are case-sensitive both have built-in regular expressions for powerful text processing differences:JS is more object-oriented: noun.verb(), less procedural: verb(noun)JS focuses on user interfaces and interacting with a document; PHP is geared toward HTML output and file/form processing JS code runs on the client's browser; PHP code runs on the web server
script tag should be placed in HTML page's head
script code is stored in a separate .js file
indexof的用法javascriptJS code can be placed directly in the HTML file's body or head (like CSS) but this is bad style (should separate content, presentation, and behavior)
a JS command that pops up a dialog box with a message
variables are declared with the var keyword (case sensitive)
types are not specified, but JS does have types ("loosely typed")
Number, Boolean, String, Array, Object, Function, Null, Undefined
can find out a variable's type by calling typeof
integers and real numbers are the same type (no int vs. double)
same operators: + - * / % ++ -- = += -= *= /= %=
similar precedence to Java
many operators auto-convert types: "2" * 3 is 6
HTML:CSS/JS/PHP:Java/JS/PHP:PHP:identical to Java's comment syntax recall: 4 comment syntaxes <!-- comment -->
/* comment */
// comment
#
comment
methods: charAt , charCodeAt , fromCharCode , indexOf , lastIndexOf ,replace , split , substring , toLowerCase , toUpperCase charAt returns a one-letter String (there is no char type)
length property (not a method as in Java)
Strings can be specified with "" or ''
concatenation with + :
1 + 1 is 2, but "1" + 1 is "11"
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论