Bài tập thực hành JavaScript - Nguyễn Hữu Tuấn

1:

2:

3:

 

doc21 trang | Chia sẻ: hienduc166 | Lượt xem: 631 | Lượt tải: 0download
Bạn đang xem trước 20 trang tài liệu Bài tập thực hành JavaScript - Nguyễn Hữu Tuấn, để xem tài liệu hoàn chỉnh bạn click vào nút TẢI VỀ ở trên
1 : 0; 
var op_dir = (document.form1.dir.checked == true) ? 1 : 0; 
var op_stat = (document.form1.stat.checked == true) ? 1 : 0; 
var op_menu = (document.form1.menu.checked == true) ? 1 : 0; 
var op_scroll = (document.form1.scroll.checked == true) ? 1 : 0; 
var op_resize = (document.form1.resize.checked == true) ? 1 : 0; 
var op_wid = document.form1.wid.value; 
var op_heigh = document.form1.heigh.value; 
var option = "toolbar="+ op_tool +",location="+ op_loc_box +",directories=" 
+ op_dir +",status="+ op_stat +",menubar="+ op_menu +",scrollbars=" 
+ op_scroll +",resizable=" + op_resize +",width=" + op_wid +",height="+ op_heigh;
var win3 = window.open("", "what_I_want", option); 
var win4 = window.open(address, "what_I_want");
}
function clear(form) { 
document.form1.wid.value=""; 
document.form1.heigh.value="";
}
// End -->
Please choose from the following selections to customize your window
: URL
: Toolbar
: Location
: Directories
: Status
: Menubar
: Scrollbars
: Resizable
: Width
: Height
10. Bµi 10 .
kiÓm tra tÝnh hîp lÖ cña th«ng tin nhËp vµo
<!-- Begin
function validate(){
var digits="0123456789"
var temp
if (document.testform.Name.value=="") {
alert("No Name !")
return false
}
if (document.testform.age.value=="") {
alert("Invalid Age !")
return false
}
for (var i=0;i<document.testform.age.value.length;i++){
temp=document.testform.age.value.substring(i,i+1)
if (digits.indexOf(temp)==-1){
alert("Invalid Age !")
return false
 }
 }
return true
}
// End -->
Name:
Age:
11. BµI tËp 11.
T¹o dßng ch÷ ch¹y trªn thanh tr¹ng th¸i:
Welcome to Total..
<!-- Begin
function scrollit(seed) {
var m1 = "Welcome to Total JavaScript 99! ";
var m2 = "..... You can find all the scripts you need here! ";
var m3 = "......Enjoy ";
var m4 = "";
var msg=m1+m2+m3+m4;
var out = " ";
var c = 1;
if (seed > 100) {
seed--;
cmd="scrollit("+seed+")";
timerTwo=window.setTimeout(cmd,100);
}
else if (seed 0) {
for (c=0 ; c < seed ; c++) {
out+=" ";
}
out+=msg;
seed--;
window.status=out;
cmd="scrollit("+seed+")";
timerTwo=window.setTimeout(cmd,100);
}
else if (seed <= 0) {
if (-seed < msg.length) {
out+=msg.substring(-seed,msg.length);
seed--;
window.status=out;
cmd="scrollit("+seed+")";
timerTwo=window.setTimeout(cmd,100);
}
else {
window.status=" ";
timerTwo=window.setTimeout("scrollit(100)",75);
 }
 }
}
// End -->
12. BµI tËp 12
T¹o dßng ch÷ ch¹y trong TextBox
 <!-- begin
 var max=0;
 function textlist()
 {
 max=textlist.arguments.length;
 for (i=0; i<max; i++)
 this[i]=textlist.arguments[i];
 }
 tl=new textlist
 ( 
 "This is a message", 
 "Another one",
 "And this will be the third",
 "And the fourth is the last !"
 );
 var x=0; pos=0;
 var l=tl[0].length;
 function textticker()
 {
 document.tickform.tickfield.value=tl[x].substring(0,pos)+"_";
 if(pos++==l) { pos=0; setTimeout("textticker()",1000); x++;
 if(x==max) x=0; l=tl[x].length; } else
 setTimeout("textticker()",50);
 }
 // end -->
E:\javascripts\scrolls\classic_.htm
13.VÝ dô 13.
T¹o ngµy th¸ng ch¹y trªn thanh tr¹ng th¸i
E:\scrolls\classic_.htm
<!-- Begin
var osd = " "
osd +="This is yet another JavaScript Scroll example ";
osd +="from the good folks at The JavaScript Source. ";
osd +="This one has the date and time at the front.";
osd +="Did you notice? It's coming around again, look! ";
osd +=" ";
var timer;
var msg = "";
function scrollMaster () {
msg = customDateSpring(new Date())
clearTimeout(timer)
msg += " " + showtime() + " " + osd
for (var i= 0; i < 100; i++){
msg = " " + msg;
}
scrollMe()
}
function scrollMe(){
window.status = msg;
msg = msg.substring(1, msg.length) + msg.substring(0,1);
timer = setTimeout("scrollMe()", 200);
}
function showtime (){
var now = new Date();
var hours= now.getHours();
var minutes= now.getMinutes();
var seconds= now.getSeconds();
var months= now.getMonth();
var dates= now.getDate();
var years= now.getYear();
var timeValue = ""
timeValue += ((months >9) ? "" : " ")
timeValue += ((dates >9) ? "" : " ")
timeValue = ( months +1)
timeValue +="/"+ dates
timeValue +="/"+ years
var ap="A.M."
if (hours == 12) {
ap = "P.M."
}
if (hours == 0) {
hours = 12
}
if(hours >= 13){
hours -= 12;
ap="P.M."
}
var timeValue2 = " " + hours
timeValue2 += ((minutes < 10) ? ":0":":") + minutes + " " + ap
return timeValue2;
}
function MakeArray(n) {
this.length = n
return this
}
monthNames = new MakeArray(12)
monthNames[1] = "Janurary"
monthNames[2] = "February"
monthNames[3] = "March"
monthNames[4] = "April"
monthNames[5] = "May"
monthNames[6] = "June"
monthNames[7] = "July"
monthNames[8] = "August"
monthNames[9] = "Sept."
monthNames[10] = "Oct."
monthNames[11] = "Nov."
monthNames[12] = "Dec."
daysNames = new MakeArray(7)
daysNames[1] = "Sunday"
daysNames[2] = "Monday"
daysNames[3] = "Tuesday"
daysNames[4] = "Wednesday"
daysNames[5] = "Thursday"
daysNames[6] = "Friday"
daysNames[7] = "Saturday"
function customDateSpring(oneDate) {
var theDay = daysNames[oneDate.getDay() +1]
var theDate =oneDate.getDate()
var theMonth = monthNames[oneDate.getMonth() +1]
var dayth="th"
if ((theDate == 1) || (theDate == 21) || (theDate == 31)) {
dayth="st";
}
if ((theDate == 2) || (theDate ==22)) {
dayth="nd";
}
if ((theDate== 3) || (theDate == 23)) {
dayth="rd";
}
return theDay + ", " + theMonth + " " + theDate + dayth + ","
}
scrollMaster();
// End -->
14. VÝ dô 14.
T¹o dßng ch÷ bay vµo thanh tr¹ng th¸I tõng ch÷ c¸i mét
<!-- Begin
function scroll(jumpSpaces,position) {
var msg = "Another JavaScript Example! Do you like it?"
var out = ""
if (killScroll) {return false} 
for (var i=0; i<position; i++){
out += msg.charAt(i)}
for (i=1;i<jumpSpaces;i++) {
out += " "}
out += msg.charAt(position)
window.status = out
if (jumpSpaces <= 1) {
position++
if (msg.charAt(position) == ' ') {
position++ }
jumpSpaces = 100-position 
}
else if (jumpSpaces > 3) {
jumpSpaces *= .75}
else {
jumpSpaces--}
if (position != msg.length) {
var cmd = "scroll(" + jumpSpaces + "," + position + ")";
scrollID = window.setTimeout(cmd,5);
} 
else {
scrolling = false
return false}
return true;
}
function startScroller() {
if (scrolling)
if (!confirm('Re-initialize snapIn?'))
return false
killScroll = true
scrolling = true
var killID = window.setTimeout('killScroll=false',6)
scrollID = window.setTimeout('scroll(100,0)',10)
return true
}
var scrollID = Object
var scrolling = false
var killScroll = false
// End -->
15.Bµi tËp 15
T¹o 3 button nh­ sau :
Create a New Window
 <input TYPE="button" VALUE="Open New Window"
 onClick="NewWin=window.open('','NewWin',
'toolbar=no,status=no,width=200,height=100'); ">
16.BµI tËp 16 
Cöa sæ tù tr­ît
var currentpos=0,alt=1,curpos1=0,curpos2=-1
function initialize(){
startit()
}
function scrollwindow(){
if (document.all)
temp=document.body.scrollTop
else
temp=window.pageYOffset
if (alt==0)
alt=1
else
alt=0
if (alt==0)
curpos1=temp
else
curpos2=temp
if (curpos1!=curpos2){
if (document.all)
currentpos=document.body.scrollTop+1
else
currentpos=window.pageYOffset+1
window.scroll(0,currentpos)
}
else{
currentpos=0
window.scroll(0,currentpos)
}
}
function startit(){
setInterval("scrollwindow()",10)
}
window.onload=initialize
17. Bµi tËp 17
T¹o Combo box cã fulldown menu
<!-- Begin
function formHandler(){
var URL = document.form.site.options[document.form.site.selectedIndex].value;
window.location.href = URL;
// End -->
}
E:\button\pushme_.htm
 Go to.... 
 Metacrawler 
 Altavista 
 Webcrawler 
 Lycos 
 The JavaScript Source 
18 Bµi tËp 18
T¹o hiÖu øng ; khi ®­a chuét vµo thÝ xuÊt hiÖn ¶nh kh¸c khi ®­a ra khái ¶nh th× hiÖn ¶nh cò
Document Title
onMouseOver
<img
SRC="_view2.gif" name="s" width="158" height="29">
onMouseOut
Pass the mouse over the images 
Check out the script! See how easy this function is.
19. Bµi tËp 19
T¹o nót bÊm khi ng­êi dïng bÊm vµo th× hiÖn m· nguån ch­¬ng tr×nh
20.Bµi tËp 20
Sö dông Cookies ®Ó ®Õm sè lÇn truy cËp trang Web
<!-- Begin
function GetCookie (name) { 
var arg = name + "="; 
var alen = arg.length; 
var clen = document.cookie.length; 
var i = 0; 
while (i < clen) {
var j = i + alen; 
if (document.cookie.substring(i, j) == arg) 
return getCookieVal (j); 
i = document.cookie.indexOf(" ", i) + 1; 
if (i == 0) break; 
} 
return null;
}
function SetCookie (name, value) { 
var argv = SetCookie.arguments; 
var argc = SetCookie.arguments.length; 
var expires = (argc > 2) ? argv[2] : null; 
var path = (argc > 3) ? argv[3] : null; 
var domain = (argc > 4) ? argv[4] : null; 
var secure = (argc > 5) ? argv[5] : false; 
document.cookie = name + "=" + escape (value) + 
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
((path == null) ? "" : ("; path=" + path)) + 
((domain == null) ? "" : ("; domain=" + domain)) + 
((secure == true) ? "; secure" : "");
}
function DeleteCookie (name) { 
var exp = new Date(); 
exp.setTime (exp.getTime() - 1); 
var cval = GetCookie (name); 
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
var expDays = 30;
var exp = new Date(); 
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
function amt(){
var count = GetCookie('count')
if(count == null) {
SetCookie('count','1')
return 1
}
else {
var newcount = parseInt(count) + 1;
DeleteCookie('count')
SetCookie('count',newcount,exp)
return count
 }
}
function getCookieVal(offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
// End -->
E:\cookies\name_.htm
<!-- Begin
document.write("You've been here " + amt() + " times.")
// End -->

File đính kèm:

  • docJAVAGT.doc
Bài giảng liên quan