SlideShare a Scribd company logo
Introduc)on	
  to	
  JavaScript	
  

            Jussi	
  Pohjolainen	
  
Tampere	
  University	
  of	
  Applied	
  Sciences	
  
JavaScript	
  
•  Object-­‐orientated	
  scrip)ng	
  language	
  
•  Dialect	
  of	
  EcmaScript-­‐standard	
  
•  History	
  
    –  Netscape:	
  LiveScript	
  to	
  JavaScript	
  
    –  MicrosoH:	
  JScript	
  
    –  Standard:	
  EcmaScript	
  
•  Latest	
  version:	
  JavaScript	
  1.8.1,	
  a	
  superset	
  of	
  
   EcmaScript	
  
Possibili)es?	
  
•  JS	
  was	
  designed	
  to	
  add	
  interac)vity	
  to	
  HTML	
  
   pages	
  
•  Dynamic	
  HTML	
  
•  Can	
  react	
  to	
  events:	
  page	
  has	
  finished	
  loading,	
  
   user	
  clicks..	
  
•  Data	
  valida)on	
  
•  Browser	
  detec)on	
  
•  Cookies	
  
Compa)bility	
  
•    Old	
  or	
  rare	
  browsers	
  
•    PDA	
  or	
  Mobile	
  phones	
  
•    JavaScript	
  execu)on	
  disabled	
  
•    The	
  use	
  of	
  speech	
  browser	
  
•    Browser	
  incompa)bilites	
  
JavaScript	
  Today:	
  AJAX	
  
•  JavaScript	
  is	
  heavily	
  used	
  in	
  AJAX-­‐based	
  sites	
  
•  AJAX:	
  asynchronous	
  JavaScript	
  and	
  XML	
  
    –  group	
  of	
  interrelated	
  techniques	
  used	
  on	
  the	
  
       client-­‐side	
  to	
  create	
  rich	
  web	
  apps	
  where	
  data	
  is	
  
       retrieved	
  from	
  the	
  server	
  in	
  the	
  background.	
  
•  Example	
  usage:	
  Gmail,	
  Google	
  Maps	
  
Google	
  Web	
  Toolkit	
  
•  Great	
  tool	
  for	
  crea)ng	
  AJAX/JS-­‐based	
  sites	
  
•  Coding	
  is	
  done	
  with	
  Java	
  which	
  is	
  compiled	
  to	
  
   JavaScript	
  
•  Resolves	
  browser	
  incompa)bilies	
  
•  See	
  Example:	
  
    –  hZp://gwt.google.com/samples/Showcase/
       Showcase.html	
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Embed Example</title>
    <meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" /
    >
</head>
<body>

<p>
<!-- See: http://covertprestige.info/html/script-syntax/ -->
<script type="text/javascript">
//<![CDATA[

document.write("Hello from JS!");

//]]>
</script>
</p>

</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>External JS Example</title>
    <meta http-equiv="content-type" content="application/xhtml
   +xml; charset=utf-8" />
   <script type="text/javascript" src="event.js"></script>
</head>
<body onload="message()">

</body>
</html>
// event.js
function message()
{
    alert("This alert box was called
  with the onload event");
}
Result	
  
QUICK	
  INTRO	
  TO	
  PROGRAMMING	
  
WITH	
  JS	
  
Variables	
  
•  Values	
  are	
  stored	
  in	
  variables	
  
•  Variables	
  are	
  declared:	
  
    –  var carname;
•  Assigning	
  value	
  
    –  carname = "volvo";
•  Together	
  
    –  var carname = "volvo";
...
<body>

<p>
<script type="text/javascript">
//<![CDATA[

// Declaration
var car;
// Assigning
car = "Volvo";
document.write(car);

//]]>
</script>
</p>

</body>
</html>
Comparison	
  (w3schools)	
  
<script type="text/javascript">
//<![CDATA[

var d    = new Date();
var time = d.getHours();

if ( time < 10 )
{
  document.write("<b>Good morning</b>");
}

//]]>
</script>
Comparison	
  (w3schools)	
  
<script type="text/javascript">
//<![CDATA[

var d    = new Date();
var time = d.getHours();

if ( time < 10 )
{
  document.write("<b>Good morning</b>");
}
else
{
  document.write("<b>Good Day</b>");
}
//]]>
</script>
Repeat	
  (w3schools)	
  
<script type="text/javascript">
//<![CDATA[

var i=0;
while (i<=5)
{
  document.write("The number is " + i);
  document.write("<br />");
  i = i + 1;
}

//]]>
</script>
Popup	
  Boxes	
  
•  Alert	
  Box	
  
    –  alert("some	
  text");	
  
•  Confirm	
  Box	
  
    –  confirm("some	
  text");	
  
•  Prompt	
  Box	
  
    –  prompt("sometext",	
  "default	
  value")	
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-
  strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Embed Example</title>
    <meta http-equiv="content-type" content="application/
  xhtml+xml; charset=utf-8" />
</head>
<body>

<input type="button" onclick="alert('moi');" value="Show
  alert box" />

</body>
</html>
Result	
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Embed Example</title>
    <meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" /
    >
    <script type="text/javascript">
    //<![CDATA[
    function showAlert()
    {
         alert("Hello World!");
    }
    //]]>
    </script>
</head>
<body>

<input type="button" onclick="showAlert();" value="Show alert box" />

</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Embed Example</title>
    <meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" />
    <script type="text/javascript">
    //<![CDATA[
    function askQuestion()
    {
         var name = prompt("Please enter your name","Harry Potter");

          if ( name!=null && name!="" )
          {
              alert("Hello " + name + "! How are you today?");
          }
    }

    //]]>
    </script>
</head>
<body>

<input type="button" onclick="askQuestion();" value="Question for me" />

</body>
</html>
JS	
  EVENTS	
  AND	
  DOM	
  
JS	
  Events	
  
•  Mouse	
  click	
  (onclick)	
  
•  Web	
  page	
  loading	
  (onload)	
  
•  Mousing	
  over	
  and	
  out	
  (onmouseover	
  
   onmouseout)	
  
•  Submiang	
  HTML	
  form	
  (onsubmit)	
  
About	
  Events	
  
•  You	
  may	
  cancel	
  some	
  events:	
  
    –  <a href=http://www.tamk.fi/
       onclick="alert('message'); return
       false;">
•  Example	
  
    –  <form name="myform" action=""
       onsubmit="return validate();">
Example	
  	
  
<form name="myform" method="post" onsubmit="return
  count()">
    Height (m):<br/>
       <input type="text" name="height"/><br/>
    Weight (kg):<br/>
       <input type="text" name="weight"/><br/>

       <input type="submit" value="BMI"/><br/>

    BMI<br/>
        <input type="text" name="result"/>
</form>
<script type="text/javascript">
   //<![CDATA[
   function count()
   {
       var height = document.myform.height.value;
       var weight = document.myform.weight.value;
       document.myform.result.value = (weight / (height*height));

        return false;
    }

    //]]>
</script>
Result	
  
DOM	
  
DOM?	
  
•  Specifica)on	
  how	
  to	
  
   access	
  (X)Html	
  –	
  elements	
  
•  Different	
  levels	
  of	
  DOM:	
  0,	
  
   1,	
  and	
  2	
  
window	
  -­‐	
  object	
  
•  Every	
  reference	
  to	
  other	
  objects	
  is	
  done	
  via	
  
   the	
  window	
  –	
  object	
  
•  You	
  don't	
  have	
  to	
  use	
  the	
  reference	
  in	
  your	
  
   code:	
  
    –  window.document.form.height.value	
  =	
  
    –  document.form.height.value	
  
•  Window	
  methods	
  
    –  alert,	
  close,	
  confirm,	
  open,	
  prompt,	
  setTimeOut	
  
Opening	
  new	
  Browser	
  Window	
  
// See:
  http://www.javascript-coder.com/window-
  popup/javascript-window-open.phtml

window.open("http://www.tamk.fi",
            "title",
            "width=600, height=100");
navigator	
  -­‐	
  object	
  
•  navigator	
  tells	
  informa)on	
  about	
  your	
  browser	
  
•  Client-­‐sniffing	
  
   var browser   = navigator.appName;
   var b_version = navigator.appVersion;
   var version   = parseFloat(b_version);

   document.write("Browser name: "+ browser);
   document.write("<br />");
   document.write("Browser version: "+ version);
document	
  -­‐	
  object	
  
•  Collec)on	
  of	
  elements	
  in	
  the	
  html-­‐page	
  
•  Crea)ng	
  Nodes	
  
    –  createElement("element	
  name")	
  
    –  createTextNode("text")	
  
•  Walk	
  the	
  Tree	
  
    –  getElementsByTagName	
  
    –  getElementById	
  
•  See:	
  hZp://www.howtocreate.co.uk/tutorials/
   javascript/domstructure	
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" />
      <script type="text/javascript">
    //<![CDATA[
    function change()
    {
         // Get list of ALL <h1> - elements
         var listOfHeading1 = window.document.getElementsByTagName("h1");

          // Find the first <h1> - element in the list
          var heading1       = listOfHeading1[0];

          // Get the child - element of the first <h1> - element (Text)
          var text           = heading1.firstChild;

          // Replace the text
          text.data = "Hello from JS!";
   }

    //]]>
</script>

</head>
<body>

<h1>Title</h1>

<input type="submit" onClick="change();" value="click!"/>

</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" />
      <script type="text/javascript">
    //<![CDATA[
    function change()
    {
         // Reference to the table - element
         var table = document.getElementById("mytable");

          var tr      = document.createElement("tr");        // <tr>
          var td1     = document.createElement("td");        // <td>
          var td1Text = document.createTextNode("New Cell"); // "New Cell"
          td1.appendChild(td1Text);                          // <td>New Cell</td>

          var td2     = document.createElement("td");        // <td>
          var td2Text = document.createTextNode("New Cell"); // "New Cell"
          td2.appendChild(td2Text);                          // <td>New Cell</td>

          tr.appendChild(td1);
          tr.appendChild(td2);

          table.appendChild(tr);
   }


    //]]>
</script>

</head>
<body>

<table id="mytable" border="1">
<tr><td>&nbsp;</td><td>&nbsp;</td></tr>
<tr><td>&nbsp;</td><td>&nbsp;</td></tr>
<tr><td>&nbsp;</td><td>&nbsp;</td></tr>
</table>

<input type="submit" onClick="change();" value="Add Row"/>

</body>
</html>

More Related Content

PPTX
Intro to Javascript
PDF
Anonymous functions in JavaScript
PPTX
Javascript basics for automation testing
PPTX
Lab #2: Introduction to Javascript
PDF
Basics of JavaScript
PDF
Advanced javascript
PDF
JavaScript Basics and Best Practices - CC FE & UX
PPT
JavaScript Tutorial
Intro to Javascript
Anonymous functions in JavaScript
Javascript basics for automation testing
Lab #2: Introduction to Javascript
Basics of JavaScript
Advanced javascript
JavaScript Basics and Best Practices - CC FE & UX
JavaScript Tutorial

What's hot (20)

PDF
Funcitonal Swift Conference: The Functional Way
PPT
Javascript
PPT
JavaScript Basics
PPTX
5 Tips for Better JavaScript
PDF
Powerful JavaScript Tips and Best Practices
PDF
Java Script Best Practices
PDF
Ten useful JavaScript tips & best practices
PPTX
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
PPTX
Javascript 101
PPT
JavaScript Functions
PPTX
JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...
PPT
Javascript built in String Functions
PDF
Fundamental JavaScript [UTC, March 2014]
PDF
Bottom Up
PDF
LetSwift RxSwift 시작하기
PDF
Headless Js Testing
PPT
Javascript
PDF
javascript objects
PDF
Understanding Asynchronous JavaScript
PDF
Javascript basics
Funcitonal Swift Conference: The Functional Way
Javascript
JavaScript Basics
5 Tips for Better JavaScript
Powerful JavaScript Tips and Best Practices
Java Script Best Practices
Ten useful JavaScript tips & best practices
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Javascript 101
JavaScript Functions
JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...
Javascript built in String Functions
Fundamental JavaScript [UTC, March 2014]
Bottom Up
LetSwift RxSwift 시작하기
Headless Js Testing
Javascript
javascript objects
Understanding Asynchronous JavaScript
Javascript basics
Ad

Viewers also liked (9)

PDF
NodeJs Intro - JavaScript Zagreb Meetup #1
PDF
Intro to javascript (4 week)
PDF
Javascript intro for MAH
PDF
JavaScript Intro
KEY
Intro to Javascript
PPTX
JavaScript - Intro
PDF
Intro to Javascript and jQuery
PDF
Intro to JavaScript
PDF
Intro to JavaScript
NodeJs Intro - JavaScript Zagreb Meetup #1
Intro to javascript (4 week)
Javascript intro for MAH
JavaScript Intro
Intro to Javascript
JavaScript - Intro
Intro to Javascript and jQuery
Intro to JavaScript
Intro to JavaScript
Ad

Similar to Intro to JavaScript (20)

PDF
Intro to JavaScript
PDF
Rich Portlet Development in uPortal
PDF
HTML 5 - Overview
TXT
smoke1272528461
PDF
Introduction to JavaScript
DOCX
Web Scripting Project JavaScripts and HTML WebPage
TXT
Private slideshow
PDF
27javascript
PDF
Javascript
TXT
Portafolio.carlos serrano grupo 201512_20
DOCX
Doctype html public
PDF
Primefaces Nextgen Lju
PDF
Primefaces Nextgen Lju
PDF
Spca2014 hillier 3rd party_javascript_libraries
PDF
An Introduction to Tornado
PDF
PDF
Introduccion a HTML5
PPSX
Introduction to Html5
PDF
Web Standards Support in WebKit
KEY
Html5 For Jjugccc2009fall
Intro to JavaScript
Rich Portlet Development in uPortal
HTML 5 - Overview
smoke1272528461
Introduction to JavaScript
Web Scripting Project JavaScripts and HTML WebPage
Private slideshow
27javascript
Javascript
Portafolio.carlos serrano grupo 201512_20
Doctype html public
Primefaces Nextgen Lju
Primefaces Nextgen Lju
Spca2014 hillier 3rd party_javascript_libraries
An Introduction to Tornado
Introduccion a HTML5
Introduction to Html5
Web Standards Support in WebKit
Html5 For Jjugccc2009fall

More from Jussi Pohjolainen (20)

PDF
Moved to Speakerdeck
PDF
Java Web Services
PDF
Box2D and libGDX
PDF
libGDX: Screens, Fonts and Preferences
PDF
libGDX: Tiled Maps
PDF
libGDX: User Input and Frame by Frame Animation
PDF
Intro to Building Android Games using libGDX
PDF
Advanced JavaScript Development
PDF
Introduction to AngularJS
PDF
libGDX: Scene2D
PDF
libGDX: Simple Frame Animation
PDF
libGDX: Simple Frame Animation
PDF
libGDX: User Input
PDF
Implementing a Simple Game using libGDX
PDF
Building Android games using LibGDX
PDF
Android Threading
PDF
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
PDF
Creating Games for Asha - platform
PDF
Intro to Asha UI
PDF
Intro to Java ME and Asha Platform
Moved to Speakerdeck
Java Web Services
Box2D and libGDX
libGDX: Screens, Fonts and Preferences
libGDX: Tiled Maps
libGDX: User Input and Frame by Frame Animation
Intro to Building Android Games using libGDX
Advanced JavaScript Development
Introduction to AngularJS
libGDX: Scene2D
libGDX: Simple Frame Animation
libGDX: Simple Frame Animation
libGDX: User Input
Implementing a Simple Game using libGDX
Building Android games using LibGDX
Android Threading
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Games for Asha - platform
Intro to Asha UI
Intro to Java ME and Asha Platform

Recently uploaded (20)

PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
RMMM.pdf make it easy to upload and study
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
Pre independence Education in Inndia.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Pharma ospi slides which help in ospi learning
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Business Ethics Teaching Materials for college
PPTX
Cell Types and Its function , kingdom of life
PPTX
Institutional Correction lecture only . . .
PDF
Basic Mud Logging Guide for educational purpose
PDF
01-Introduction-to-Information-Management.pdf
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
RMMM.pdf make it easy to upload and study
TR - Agricultural Crops Production NC III.pdf
human mycosis Human fungal infections are called human mycosis..pptx
Week 4 Term 3 Study Techniques revisited.pptx
Pre independence Education in Inndia.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
O7-L3 Supply Chain Operations - ICLT Program
STATICS OF THE RIGID BODIES Hibbelers.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Microbial diseases, their pathogenesis and prophylaxis
Pharma ospi slides which help in ospi learning
Microbial disease of the cardiovascular and lymphatic systems
Business Ethics Teaching Materials for college
Cell Types and Its function , kingdom of life
Institutional Correction lecture only . . .
Basic Mud Logging Guide for educational purpose
01-Introduction-to-Information-Management.pdf

Intro to JavaScript

  • 1. Introduc)on  to  JavaScript   Jussi  Pohjolainen   Tampere  University  of  Applied  Sciences  
  • 2. JavaScript   •  Object-­‐orientated  scrip)ng  language   •  Dialect  of  EcmaScript-­‐standard   •  History   –  Netscape:  LiveScript  to  JavaScript   –  MicrosoH:  JScript   –  Standard:  EcmaScript   •  Latest  version:  JavaScript  1.8.1,  a  superset  of   EcmaScript  
  • 3. Possibili)es?   •  JS  was  designed  to  add  interac)vity  to  HTML   pages   •  Dynamic  HTML   •  Can  react  to  events:  page  has  finished  loading,   user  clicks..   •  Data  valida)on   •  Browser  detec)on   •  Cookies  
  • 4. Compa)bility   •  Old  or  rare  browsers   •  PDA  or  Mobile  phones   •  JavaScript  execu)on  disabled   •  The  use  of  speech  browser   •  Browser  incompa)bilites  
  • 5. JavaScript  Today:  AJAX   •  JavaScript  is  heavily  used  in  AJAX-­‐based  sites   •  AJAX:  asynchronous  JavaScript  and  XML   –  group  of  interrelated  techniques  used  on  the   client-­‐side  to  create  rich  web  apps  where  data  is   retrieved  from  the  server  in  the  background.   •  Example  usage:  Gmail,  Google  Maps  
  • 6. Google  Web  Toolkit   •  Great  tool  for  crea)ng  AJAX/JS-­‐based  sites   •  Coding  is  done  with  Java  which  is  compiled  to   JavaScript   •  Resolves  browser  incompa)bilies   •  See  Example:   –  hZp://gwt.google.com/samples/Showcase/ Showcase.html  
  • 7. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Embed Example</title> <meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" / > </head> <body> <p> <!-- See: http://covertprestige.info/html/script-syntax/ --> <script type="text/javascript"> //<![CDATA[ document.write("Hello from JS!"); //]]> </script> </p> </body> </html>
  • 8. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>External JS Example</title> <meta http-equiv="content-type" content="application/xhtml +xml; charset=utf-8" /> <script type="text/javascript" src="event.js"></script> </head> <body onload="message()"> </body> </html>
  • 9. // event.js function message() { alert("This alert box was called with the onload event"); }
  • 11. QUICK  INTRO  TO  PROGRAMMING   WITH  JS  
  • 12. Variables   •  Values  are  stored  in  variables   •  Variables  are  declared:   –  var carname; •  Assigning  value   –  carname = "volvo"; •  Together   –  var carname = "volvo";
  • 13. ... <body> <p> <script type="text/javascript"> //<![CDATA[ // Declaration var car; // Assigning car = "Volvo"; document.write(car); //]]> </script> </p> </body> </html>
  • 14. Comparison  (w3schools)   <script type="text/javascript"> //<![CDATA[ var d = new Date(); var time = d.getHours(); if ( time < 10 ) {   document.write("<b>Good morning</b>"); } //]]> </script>
  • 15. Comparison  (w3schools)   <script type="text/javascript"> //<![CDATA[ var d = new Date(); var time = d.getHours(); if ( time < 10 ) {   document.write("<b>Good morning</b>"); } else {   document.write("<b>Good Day</b>"); } //]]> </script>
  • 16. Repeat  (w3schools)   <script type="text/javascript"> //<![CDATA[ var i=0; while (i<=5) {   document.write("The number is " + i);   document.write("<br />");   i = i + 1; } //]]> </script>
  • 17. Popup  Boxes   •  Alert  Box   –  alert("some  text");   •  Confirm  Box   –  confirm("some  text");   •  Prompt  Box   –  prompt("sometext",  "default  value")  
  • 18. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1- strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Embed Example</title> <meta http-equiv="content-type" content="application/ xhtml+xml; charset=utf-8" /> </head> <body> <input type="button" onclick="alert('moi');" value="Show alert box" /> </body> </html>
  • 20. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Embed Example</title> <meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" / > <script type="text/javascript"> //<![CDATA[ function showAlert() { alert("Hello World!"); } //]]> </script> </head> <body> <input type="button" onclick="showAlert();" value="Show alert box" /> </body> </html>
  • 21. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Embed Example</title> <meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" /> <script type="text/javascript"> //<![CDATA[ function askQuestion() { var name = prompt("Please enter your name","Harry Potter"); if ( name!=null && name!="" ) { alert("Hello " + name + "! How are you today?"); } } //]]> </script> </head> <body> <input type="button" onclick="askQuestion();" value="Question for me" /> </body> </html>
  • 22. JS  EVENTS  AND  DOM  
  • 23. JS  Events   •  Mouse  click  (onclick)   •  Web  page  loading  (onload)   •  Mousing  over  and  out  (onmouseover   onmouseout)   •  Submiang  HTML  form  (onsubmit)  
  • 24. About  Events   •  You  may  cancel  some  events:   –  <a href=http://www.tamk.fi/ onclick="alert('message'); return false;"> •  Example   –  <form name="myform" action="" onsubmit="return validate();">
  • 25. Example     <form name="myform" method="post" onsubmit="return count()"> Height (m):<br/> <input type="text" name="height"/><br/> Weight (kg):<br/> <input type="text" name="weight"/><br/> <input type="submit" value="BMI"/><br/> BMI<br/> <input type="text" name="result"/> </form>
  • 26. <script type="text/javascript"> //<![CDATA[ function count() { var height = document.myform.height.value; var weight = document.myform.weight.value; document.myform.result.value = (weight / (height*height)); return false; } //]]> </script>
  • 29. DOM?   •  Specifica)on  how  to   access  (X)Html  –  elements   •  Different  levels  of  DOM:  0,   1,  and  2  
  • 30. window  -­‐  object   •  Every  reference  to  other  objects  is  done  via   the  window  –  object   •  You  don't  have  to  use  the  reference  in  your   code:   –  window.document.form.height.value  =   –  document.form.height.value   •  Window  methods   –  alert,  close,  confirm,  open,  prompt,  setTimeOut  
  • 31. Opening  new  Browser  Window   // See: http://www.javascript-coder.com/window- popup/javascript-window-open.phtml window.open("http://www.tamk.fi", "title", "width=600, height=100");
  • 32. navigator  -­‐  object   •  navigator  tells  informa)on  about  your  browser   •  Client-­‐sniffing   var browser = navigator.appName; var b_version = navigator.appVersion; var version = parseFloat(b_version); document.write("Browser name: "+ browser); document.write("<br />"); document.write("Browser version: "+ version);
  • 33. document  -­‐  object   •  Collec)on  of  elements  in  the  html-­‐page   •  Crea)ng  Nodes   –  createElement("element  name")   –  createTextNode("text")   •  Walk  the  Tree   –  getElementsByTagName   –  getElementById   •  See:  hZp://www.howtocreate.co.uk/tutorials/ javascript/domstructure  
  • 34. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" /> <script type="text/javascript"> //<![CDATA[ function change() { // Get list of ALL <h1> - elements var listOfHeading1 = window.document.getElementsByTagName("h1"); // Find the first <h1> - element in the list var heading1 = listOfHeading1[0]; // Get the child - element of the first <h1> - element (Text) var text = heading1.firstChild; // Replace the text text.data = "Hello from JS!"; } //]]> </script> </head> <body> <h1>Title</h1> <input type="submit" onClick="change();" value="click!"/> </body> </html>
  • 35. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" /> <script type="text/javascript"> //<![CDATA[ function change() { // Reference to the table - element var table = document.getElementById("mytable"); var tr = document.createElement("tr"); // <tr> var td1 = document.createElement("td"); // <td> var td1Text = document.createTextNode("New Cell"); // "New Cell" td1.appendChild(td1Text); // <td>New Cell</td> var td2 = document.createElement("td"); // <td> var td2Text = document.createTextNode("New Cell"); // "New Cell" td2.appendChild(td2Text); // <td>New Cell</td> tr.appendChild(td1); tr.appendChild(td2); table.appendChild(tr); } //]]> </script> </head> <body> <table id="mytable" border="1"> <tr><td>&nbsp;</td><td>&nbsp;</td></tr> <tr><td>&nbsp;</td><td>&nbsp;</td></tr> <tr><td>&nbsp;</td><td>&nbsp;</td></tr> </table> <input type="submit" onClick="change();" value="Add Row"/> </body> </html>