SlideShare a Scribd company logo
1 of 13
HFJS – Book
Review Ch3
Brady Cheng
Agenda
 Timer
 Display   Size
 Cookie
Timer
 One   shot timer




 Interval   timer (loop timer)
One Shot Timer
 setTimeout     ( “timeout action” , delay );


    /*                                          Millisecond!
     *    pop up an alert window after 5 sec
     */
          setTimeout("alert('123')", 5*1000);
Interval Timer
 varTimerID = setInterval ( “timeout action” ,
                              delay);
 clearInterval ( TimerID);
                                                            Millisecond!
        var timer;
        function startIntervalTimer()
        {
              timer = setInterval("alert('hi')", 3*1000);
        }
        function stopIntervalTimer()
        {
              clearInterval(timer);
        }
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();”>
Display Size
 Example     1
  function showDisplaySize()
  {
     var width = document.body.clientWidth;
     var height = document.body.clientHeight;
     alert("width = " + width + ", height = " + height);
   }
Display Size
 Example     2
function resizeEvent()
{
  var new_width2 = document.body.clientWidth;
  alert("resize width ratio ="+ old_width/new_width);
}
…
<body onload="showDisplaySize();" onresize = "resizeEvent()">
Cookie
 Data Persistence
 What can JS do?
    readCookie
    writeCookie
    eraseCookie
Cookie
 Check   whether the cookie is supported

     if(navigator.cookieEnabled)
     {
        //do something
     }
     else
     {
                                    JS build-in
        //do other things
                                      object
     }
Fmt
             example


  Cookie
    Write   cookie          Fmt : name=XXX ; expires=someday

function 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=/";
}
Cookie
   Read     cookie
function 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;
}
Cookie
 Erase   cookie

   function eraseCookie(name)
   {
      writeCookie(name, “”, -1);
   }

More Related Content

What's hot

Building mobile apps with Windows Azure mobile services
Building mobile apps with Windows Azure mobile servicesBuilding mobile apps with Windows Azure mobile services
Building mobile apps with Windows Azure mobile services
Mobile Monday Srbija
 
Qtp Imp Script Examples
Qtp Imp Script ExamplesQtp Imp Script Examples
Qtp Imp Script Examples
User1test
 

What's hot (12)

PHP Cookies and Sessions
PHP Cookies and SessionsPHP Cookies and Sessions
PHP Cookies and Sessions
 
PHP - Getting good with cookies
PHP - Getting good with cookiesPHP - Getting good with cookies
PHP - Getting good with cookies
 
Php ssession - cookies -introduction
Php ssession - cookies -introductionPhp ssession - cookies -introduction
Php ssession - cookies -introduction
 
Building mobile apps with Windows Azure mobile services
Building mobile apps with Windows Azure mobile servicesBuilding mobile apps with Windows Azure mobile services
Building mobile apps with Windows Azure mobile services
 
Qtp Imp Script Examples
Qtp Imp Script ExamplesQtp Imp Script Examples
Qtp Imp Script Examples
 
Making Games in JavaScript
Making Games in JavaScriptMaking Games in JavaScript
Making Games in JavaScript
 
c++ boost and STL
c++  boost and STLc++  boost and STL
c++ boost and STL
 
Cервер на Go для мобильной стратегии
Cервер на Go для мобильной стратегииCервер на Go для мобильной стратегии
Cервер на Go для мобильной стратегии
 
PHP Cookies, Sessions and Authentication
PHP Cookies, Sessions and AuthenticationPHP Cookies, Sessions and Authentication
PHP Cookies, Sessions and Authentication
 
Fluent plugin-dstat
Fluent plugin-dstatFluent plugin-dstat
Fluent plugin-dstat
 
Cookies
CookiesCookies
Cookies
 
PHP - Introduction to PHP Cookies and Sessions
PHP - Introduction to PHP Cookies and SessionsPHP - Introduction to PHP Cookies and Sessions
PHP - Introduction to PHP Cookies and Sessions
 

Viewers also liked (9)

Javascript ch8
Javascript ch8Javascript ch8
Javascript ch8
 
Ruby introduction part1
Ruby introduction part1Ruby introduction part1
Ruby introduction part1
 
Javascipt ch1
Javascipt ch1Javascipt ch1
Javascipt ch1
 
Javascript ch2
Javascript ch2Javascript ch2
Javascript ch2
 
Javascipt ch4 & ch5
Javascipt ch4 & ch5Javascipt ch4 & ch5
Javascipt ch4 & ch5
 
RoR guide_p1
RoR guide_p1RoR guide_p1
RoR guide_p1
 
Javascript ch6
Javascript ch6Javascript ch6
Javascript ch6
 
design pattern overview
design pattern overviewdesign pattern overview
design pattern overview
 
Javascript ch7
Javascript ch7Javascript ch7
Javascript ch7
 

Similar to Javascript ch3

01 Introduction - JavaScript Development
01 Introduction - JavaScript Development01 Introduction - JavaScript Development
01 Introduction - JavaScript Development
Tommy Vercety
 
Is HTML5 Ready? (workshop)
Is HTML5 Ready? (workshop)Is HTML5 Ready? (workshop)
Is HTML5 Ready? (workshop)
Remy Sharp
 
Is html5-ready-workshop-110727181512-phpapp02
Is html5-ready-workshop-110727181512-phpapp02Is html5-ready-workshop-110727181512-phpapp02
Is html5-ready-workshop-110727181512-phpapp02
PL dream
 
HTML5 & The Open Web - at Nackademin
HTML5 & The Open Web -  at NackademinHTML5 & The Open Web -  at Nackademin
HTML5 & The Open Web - at Nackademin
Robert Nyman
 
YUI for control freaks - a presentation at The Ajax Experience
YUI for control freaks - a presentation at The Ajax ExperienceYUI for control freaks - a presentation at The Ajax Experience
YUI for control freaks - a presentation at The Ajax Experience
Christian Heilmann
 
Java script frame window
Java script frame windowJava script frame window
Java script frame window
H K
 
Deep dumpster diving 2010
Deep dumpster diving 2010Deep dumpster diving 2010
Deep dumpster diving 2010
RonnBlack
 

Similar to Javascript ch3 (20)

JavaScript Refactoring
JavaScript RefactoringJavaScript Refactoring
JavaScript Refactoring
 
Asp.net state management
Asp.net state managementAsp.net state management
Asp.net state management
 
Javascript 2
Javascript 2Javascript 2
Javascript 2
 
Google App Engine Developer - Day3
Google App Engine Developer - Day3Google App Engine Developer - Day3
Google App Engine Developer - Day3
 
Javascript 1
Javascript 1Javascript 1
Javascript 1
 
Qtp test
Qtp testQtp test
Qtp test
 
Javascript1
Javascript1Javascript1
Javascript1
 
01 Introduction - JavaScript Development
01 Introduction - JavaScript Development01 Introduction - JavaScript Development
01 Introduction - JavaScript Development
 
Is HTML5 Ready? (workshop)
Is HTML5 Ready? (workshop)Is HTML5 Ready? (workshop)
Is HTML5 Ready? (workshop)
 
Is html5-ready-workshop-110727181512-phpapp02
Is html5-ready-workshop-110727181512-phpapp02Is html5-ready-workshop-110727181512-phpapp02
Is html5-ready-workshop-110727181512-phpapp02
 
Ten useful JavaScript tips & best practices
Ten useful JavaScript tips & best practicesTen useful JavaScript tips & best practices
Ten useful JavaScript tips & best practices
 
Async Testing giving you a sinking feeling
Async Testing giving you a sinking feelingAsync Testing giving you a sinking feeling
Async Testing giving you a sinking feeling
 
jQuery secrets
jQuery secretsjQuery secrets
jQuery secrets
 
HTML5 & The Open Web - at Nackademin
HTML5 & The Open Web -  at NackademinHTML5 & The Open Web -  at Nackademin
HTML5 & The Open Web - at Nackademin
 
Writing Maintainable JavaScript
Writing Maintainable JavaScriptWriting Maintainable JavaScript
Writing Maintainable JavaScript
 
YUI for control freaks - a presentation at The Ajax Experience
YUI for control freaks - a presentation at The Ajax ExperienceYUI for control freaks - a presentation at The Ajax Experience
YUI for control freaks - a presentation at The Ajax Experience
 
Java script frame window
Java script frame windowJava script frame window
Java script frame window
 
Introdução à programação orientada para aspectos
Introdução à programação orientada para aspectosIntrodução à programação orientada para aspectos
Introdução à programação orientada para aspectos
 
Moustamera
MoustameraMoustamera
Moustamera
 
Deep dumpster diving 2010
Deep dumpster diving 2010Deep dumpster diving 2010
Deep dumpster diving 2010
 

Javascript ch3

  • 1. HFJS – Book Review Ch3 Brady Cheng
  • 3. Timer  One shot timer  Interval timer (loop timer)
  • 4. One Shot Timer  setTimeout ( “timeout action” , delay ); /* Millisecond! * pop up an alert window after 5 sec */ setTimeout("alert('123')", 5*1000);
  • 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. 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. Display Size  Example 1 function showDisplaySize() { var width = document.body.clientWidth; var height = document.body.clientHeight; alert("width = " + width + ", height = " + height); }
  • 8. Display Size  Example 2 function resizeEvent() { var new_width2 = document.body.clientWidth; alert("resize width ratio ="+ old_width/new_width); } … <body onload="showDisplaySize();" onresize = "resizeEvent()">
  • 9. Cookie  Data Persistence  What can JS do?  readCookie  writeCookie  eraseCookie
  • 10. Cookie  Check whether the cookie is supported if(navigator.cookieEnabled) { //do something } else { JS build-in //do other things object }
  • 11. Fmt example Cookie  Write cookie Fmt : name=XXX ; expires=someday function 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. Cookie  Read cookie function 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. Cookie  Erase cookie function eraseCookie(name) { writeCookie(name, “”, -1); }