JavaScript 1.2 :
Abort, Blur, Click, Change, Focus, DblClick, DragDrop, Error KeyDown, KeyPress, KeyUp, Load, MouseDown, MouseMove, MouseOut, MouseOver, MouseUp,Move Reset, Resize, Select, Submit, Unload.
Event.
JavaScript 1.2 Event. , . , - , Event . , , :
<html>
<head>
<script language = "javascript">
function whichButton(event)
{
if (event.button == 2)
{
alert(" !");
}
else
{
alert(" !");
}
}
</script>
</head>
<body onmousedown="whichButton(event)">
<p> </p>
</body>
</html>
. . , , . :
<layer>
<a href="#" onClick="alert('x:'+event.x+'y:'+event.y);
return false;">
<img src="davinci.jpg" width=209 height=264 border=0></a>
</layer>
<a> onClick, JavaScript. , event.x event.y. Event, , .
<layer>. , .. . . ( return false; , )
Event :
Data - URL , DragDrop.
LayerX - ( ) . Resize .
LayerY - ( ) . Resize .
modifiers - , - ALT_MASK, CONTROL_MASK, META_MASK or SHIFT_MASK
pageX - ( ) .
pageY - ( ) .
screenX- ( ) .
screenY - ( ) .
target - , , .
|
|
type - , .
which - ASCII- .
x - layerX.
y - layerY.
.
- . -, , , onClick, . , , , , , . , , .
, , :
<html>
<head>
<script language="JavaScript">
window.captureEvents(Event.CLICK);
window.onclick= handle;
function handle(e) {
alert(" window !");
return true; // ..
}
</script>
</head>
<body>
<a href="test.htm">Click on this link</a>
</body>
</html>
, <a>. window.captureEvents(Event.CLICK); , Click window. window Click. , , window. , Event.CLICK CLICK . , |. :
window.captureEvents(Event.CLICK | Event.MOVE);
handle(), , return true;. , , , handle(). return false;, .
<a> onClick, , . , window , link. handle()
function handle(e) {
alert("The window object captured this event!");
window.routeEvent(e);
return true;
}
, . e - Event, .
, - . handleEvent(). :
<html>
<script language="JavaScript">
window.captureEvents(Event.CLICK);
window.onclick= handle;
function handle(e) {
document.links[1].handleEvent(e);
}
</script>
<a href="test.htm">"" </a><br>
<a href="test.htm"
onClick="alert(' !');"> </a>
|
|
</html>
Click, - !
, . - , .
<html>
<script language="JavaScript">
window.captureEvents(Event.KEYPRESS);
window.onkeypress= pressed;
function pressed(e) {
alert("Key pressed! ASCII-value: " + e.which);
}
</script>
</html>
: throw/catch/finally
JavaScript , C++ Java:
try {
//...
throw {message: "err!"}
//..
}
catch (e) {
alert("!"+e)
}
try{} . :
catch (e) {}
throw {} , try. Error:
throw new Error("server timeout")
. catch , :
try {
// ...
} catch(e) {
//
if (e instanceof ConnectionError) {
//
//reconnect
} else {
//
throw e
}
} finally {
// finally ,
// -
}
finally, javascript java. try..catch..finally, finally try/catch, , .
<!-- pr16 -->
<html>
<head>
<title> </title>
</head>
<body>
<script type="text/javascript">
function showErrorInfo(e) {
document.write(e, ":<BR>");
document.write(" : ", (e.number >> 16) & 0x1FFF, "<BR>");
document.write(" : ", e.number & 0xFFFF, "<BR>");
document.write(" : ", e.description);
}
var x;
try {
x = y; // : y
}
catch (e) { // e Error
showErrorInfo(e);
}
</script>
</body>
</html>
:
[objectError]: _:10 _:5009 : 'y' -
JavaScript :
, Object;
JavaScript: String ; Array ; Date ; Math ;
: window ; location , , , URL; history , ; navigator ;
, frames, screen setTimeout() setInterval().
, HTML CSS DOM HTML CSS document, ; document innerText, innerHTML, textContent, HTML-.
: getElementById(), getElementByTopName.
|
|
, . :
function Book() {
}
book :
var myBook = new Book();
, this, , :
function Book() {
this.paper = true;
}
var myBook = new Book();
alert(myBook.paper); // true
, .
function Book(isPaper) {//
if (isPaper) this.paper = true;
else this.paper = false;
}
var myBook = new Book(false);
alert(myBook.paper); // false
prototype, . :
Book.prototype.paper = false;
Book.prototype.isPaperBook = function() {
if (this.paper) alert("This is a paper book");
else alert("This is not a paper book");
}
function Book(isPaper) {
if (isPaper) this.paper = true;
}
var myBook = new Book(true);
myBook.isPaperBook(); // This is a paper book
, , , paper false. , true.
- JavaScript
Object JavaScript "". " => " . :
var o = new Object()
var o = {}
. - , - :
o.test = 5//
o["test"] = 5
var name = 'test'
o[name] = 5
"test" , 5.
:
alert(o.test)
alert(o['test'])
, 'undefined'
:
var h = new Object();
h.property = 10;
var s = 'ty';
alert(h['proper' + s]);// , h.property.
delete:
var h = new Object();
h['property'] = 10;
alert(delete h['property']);
// true
alert(h['property']);
// undefined
:
var h = new Object();
...
var props = '';
for(var i in h)
props += i + ': ' + h[i] + '\n';
alert(props);
, i , .