Giới thiệu Java Script
Các ví dụ
Ví dụ 1
<script language="JavaScript">
<!--
function click() {
if (event.button==2)
{
alert('Xin loi ! Ban khong the nhap nut phai duoc !');
}
}
document.onmousedown=click
// -->
</script>
Ví dụ 2
<script language="javascript">
function bgcolor(obj,state)
{
if (state=="over")
{
obj.borderColor='#ff0000';
obj.style.backgroundColor='#ccccff';
}
else
{
obj.borderColor='#ffffff';
obj.style.backgroundColor='#ffffff';
}
}
</script>
<!--------------------------------->
<table border="1" width="20%" id="table1" cellspacing="0" cellpadding="0"
bordercolorlight="#FFFFFF" bordercolordark="#FFFFFF">
<tr onmouseover="bgcolor(this,'over')" onmouseout="bgcolor(this,'out')">
<td><a href="index.aspx">Trang chủ</a></td>
</tr>
<tr onmouseover="bgcolor(this,'over')" onmouseout="bgcolor(this,'out')">
<td>Tin tức mới nhất</td>
</tr>
<tr onmouseover="bgcolor(this,'over')" onmouseout="bgcolor(this,'out')">
<td>Sự kiện</td>
</tr>
</table>
1
Ví dụ 3
<script LANGUAGE="JavaScript">
<!--
dir="Images/";
menu1a = new Image(); menu1a.src = ""+dir+"lstt.gif";
menu1b = new Image(); menu1b.src = ""+dir+"lstt1.gif";
menu2a = new Image(); menu2a.src = ""+dir+"cctc.gif";
menu2b = new Image(); menu2b.src = ""+dir+"cctc1.gif";
menu3a = new Image(); menu3a.src = ""+dir+"ttmn.gif";
menu3b = new Image(); menu3b.src = ""+dir+"ttmn1.gif";
function active(imgName)
{
img2 = eval(imgName + "a.src");
document [imgName].src = img2;
}
function inactive(imgName)
{
img3 = eval(imgName + "b.src");
document [imgName].src = img3;
}
// -->
</script>
Khi gọi hàm
<a href="" target="_self" onmouseover="inactive('menu1')"
onmouseout="active('menu1')"><img src="Image/tctv.gif" width="155"
height="25" border="0" name='menu1'></a>
Ví dụ 4:
javascript:history.back()
Ví dụ 5:
<script language="JavaScript">
var x = 0
var y = 0
var z = 140
var delay = 70
var text = "Hello and welcome to Vinh University"
while (y ++ < z) {text = " " + text }
function scroller() {
window.status = text.substring(x++, text.length);
if (x == text.length) { x = 0; }
setTimeout("scroller()", delay);
}
scroller();
//-->
</script>
2