Javascript ch3

498 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
498
On SlideShare
0
From Embeds
0
Number of Embeds
97
Actions
Shares
0
Downloads
6
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Javascript ch3

  1. 1. HFJS – BookReview Ch3Brady Cheng
  2. 2. Agenda Timer Display Size Cookie
  3. 3. Timer One shot timer Interval timer (loop timer)
  4. 4. One Shot Timer setTimeout ( “timeout action” , delay ); /* Millisecond! * pop up an alert window after 5 sec */ setTimeout("alert(123)", 5*1000);
  5. 5. Interval Timer varTimerID = setInterval ( “timeout action” , delay); clearInterval ( TimerID); Millisecond! var timer; function startIntervalTimer() { timer = setInterval("alert(hi)", 3*1000); } function stopIntervalTimer() { clearInterval(timer); }
  6. 6. Display Size Static  Physical size of your monitor  Document.body.clientHeight  Document.body.clientWeigth Dynamic  try to scroll the mid-button in your mouse while pressing ctrl button  onresizeevent  Ex. <body onresize = “doSomething();”>
  7. 7. Display Size Example 1 function showDisplaySize() { var width = document.body.clientWidth; var height = document.body.clientHeight; alert("width = " + width + ", height = " + height); }
  8. 8. Display Size Example 2function resizeEvent(){ var new_width2 = document.body.clientWidth; alert("resize width ratio ="+ old_width/new_width);}…<body onload="showDisplaySize();" onresize = "resizeEvent()">
  9. 9. Cookie Data Persistence What can JS do?  readCookie  writeCookie  eraseCookie
  10. 10. Cookie Check whether the cookie is supported if(navigator.cookieEnabled) { //do something } else { JS build-in //do other things object }
  11. 11. Fmt example Cookie  Write cookie Fmt : name=XXX ; expires=somedayfunction writeCookie(name, value, days){ var expires = “”; if (days) { var date = new Date(); date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); expires = "; expires=" + date.toGMTString(); } document.cookie = name + "=" + value + expires + "; path=/";}
  12. 12. Cookie  Read cookiefunction readCookie(name){ var searchName = name + "="; var cookies = document.cookie.split(;); //array for(var i=0; i < cookies.length; i++) { var c = cookies[i]; while (c.charAt(0) == ) c = c.substring(1, c.length); //ignore the white space if (c.indexOf(searchName) == 0) //cookie exist return c.substring(searchName.length, c.length); } return null;}
  13. 13. Cookie Erase cookie function eraseCookie(name) { writeCookie(name, “”, -1); }

×