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

What's hot (20)

PDF
Funcitonal Swift Conference: The Functional Way
Natasha Murashev
 
PPT
Javascript
mussawir20
 
PPT
JavaScript Basics
Mats Bryntse
 
PPTX
5 Tips for Better JavaScript
Todd Anglin
 
PDF
Powerful JavaScript Tips and Best Practices
Dragos Ionita
 
PDF
Java Script Best Practices
Enrique Juan de Dios
 
PDF
Ten useful JavaScript tips & best practices
Ankit Rastogi
 
PPTX
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Domenic Denicola
 
PPTX
Javascript 101
Shlomi Komemi
 
PPT
JavaScript Functions
Reem Alattas
 
PPTX
JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...
Doug Jones
 
PPT
Javascript built in String Functions
Avanitrambadiya
 
PDF
Fundamental JavaScript [UTC, March 2014]
Aaron Gustafson
 
PDF
Bottom Up
Brian Moschel
 
PDF
LetSwift RxSwift 시작하기
Wanbok Choi
 
PDF
Headless Js Testing
Brian Moschel
 
PPT
Javascript
Manav Prasad
 
PDF
javascript objects
Vijay Kalyan
 
PDF
Understanding Asynchronous JavaScript
jnewmanux
 
PDF
Javascript basics
shreesenthil
 
Funcitonal Swift Conference: The Functional Way
Natasha Murashev
 
Javascript
mussawir20
 
JavaScript Basics
Mats Bryntse
 
5 Tips for Better JavaScript
Todd Anglin
 
Powerful JavaScript Tips and Best Practices
Dragos Ionita
 
Java Script Best Practices
Enrique Juan de Dios
 
Ten useful JavaScript tips & best practices
Ankit Rastogi
 
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Domenic Denicola
 
Javascript 101
Shlomi Komemi
 
JavaScript Functions
Reem Alattas
 
JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...
Doug Jones
 
Javascript built in String Functions
Avanitrambadiya
 
Fundamental JavaScript [UTC, March 2014]
Aaron Gustafson
 
Bottom Up
Brian Moschel
 
LetSwift RxSwift 시작하기
Wanbok Choi
 
Headless Js Testing
Brian Moschel
 
Javascript
Manav Prasad
 
javascript objects
Vijay Kalyan
 
Understanding Asynchronous JavaScript
jnewmanux
 
Javascript basics
shreesenthil
 

Viewers also liked (9)

PDF
NodeJs Intro - JavaScript Zagreb Meetup #1
Tomislav Capan
 
PDF
Intro to javascript (4 week)
Jamal Sinclair O'Garro
 
PDF
Javascript intro for MAH
Aleksander Fabijan
 
PDF
JavaScript Intro
Eric Brown
 
KEY
Intro to Javascript
Kevin Ball
 
PPTX
JavaScript - Intro
Anton Tibblin
 
PDF
Intro to Javascript and jQuery
Shawn Calvert
 
PDF
Intro to JavaScript
Dan Phiffer
 
PDF
Intro to JavaScript
Yakov Fain
 
NodeJs Intro - JavaScript Zagreb Meetup #1
Tomislav Capan
 
Intro to javascript (4 week)
Jamal Sinclair O'Garro
 
Javascript intro for MAH
Aleksander Fabijan
 
JavaScript Intro
Eric Brown
 
Intro to Javascript
Kevin Ball
 
JavaScript - Intro
Anton Tibblin
 
Intro to Javascript and jQuery
Shawn Calvert
 
Intro to JavaScript
Dan Phiffer
 
Intro to JavaScript
Yakov Fain
 
Ad

Similar to Intro to JavaScript (20)

PDF
Intro to JavaScript
Jussi Pohjolainen
 
PPT
JavaScript Training
Ramindu Deshapriya
 
PPT
Javascript1
anas Mohtaseb
 
PPTX
Java Script basics and DOM
Sukrit Gupta
 
PPT
Js mod1
VARSHAKUMARI49
 
PPTX
unit4 wp.pptxjvlbpuvghuigv8ytg2ugvugvuygv
utsavsd11
 
PPTX
JavaScript lesson 1.pptx
MuqaddarNiazi1
 
PPTX
BITM3730 10-3.pptx
MattMarino13
 
PPTX
BITM3730 10-4.pptx
MattMarino13
 
PPTX
BITM3730 10-17.pptx
MattMarino13
 
PPT
Java script
Soham Sengupta
 
PPTX
JavaScripts & jQuery
Asanka Indrajith
 
PDF
JavaScript
tutorialsruby
 
PDF
JavaScript
tutorialsruby
 
PPTX
CSC PPT 12.pptx
DrRavneetSingh
 
PPT
Learn javascript easy steps
prince Loffar
 
PPTX
Java Script (Module 1).pptx
Shehrevar Davierwala
 
PPT
Javascript 2009
borkweb
 
PDF
WT UNIT 2 presentation :client side technologies JavaScript And Dom
SrushtiGhise
 
PDF
Java script
Ramesh Kumar
 
Intro to JavaScript
Jussi Pohjolainen
 
JavaScript Training
Ramindu Deshapriya
 
Javascript1
anas Mohtaseb
 
Java Script basics and DOM
Sukrit Gupta
 
unit4 wp.pptxjvlbpuvghuigv8ytg2ugvugvuygv
utsavsd11
 
JavaScript lesson 1.pptx
MuqaddarNiazi1
 
BITM3730 10-3.pptx
MattMarino13
 
BITM3730 10-4.pptx
MattMarino13
 
BITM3730 10-17.pptx
MattMarino13
 
Java script
Soham Sengupta
 
JavaScripts & jQuery
Asanka Indrajith
 
JavaScript
tutorialsruby
 
JavaScript
tutorialsruby
 
CSC PPT 12.pptx
DrRavneetSingh
 
Learn javascript easy steps
prince Loffar
 
Java Script (Module 1).pptx
Shehrevar Davierwala
 
Javascript 2009
borkweb
 
WT UNIT 2 presentation :client side technologies JavaScript And Dom
SrushtiGhise
 
Java script
Ramesh Kumar
 
Ad

More from Jussi Pohjolainen (20)

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

Recently uploaded (20)

PPTX
TRANSLATIONAL AND ROTATIONAL MOTION.pptx
KIPAIZAGABAWA1
 
PPTX
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PPTX
How to Manage Allocation Report for Manufacturing Orders in Odoo 18
Celine George
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
STATEMENT-BY-THE-HON.-MINISTER-FOR-HEALTH-ON-THE-COVID-19-OUTBREAK-AT-UG_revi...
nservice241
 
PDF
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PDF
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PPTX
infertility, types,causes, impact, and management
Ritu480198
 
PDF
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PDF
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PDF
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
TRANSLATIONAL AND ROTATIONAL MOTION.pptx
KIPAIZAGABAWA1
 
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
How to Manage Allocation Report for Manufacturing Orders in Odoo 18
Celine George
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
STATEMENT-BY-THE-HON.-MINISTER-FOR-HEALTH-ON-THE-COVID-19-OUTBREAK-AT-UG_revi...
nservice241
 
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
infertility, types,causes, impact, and management
Ritu480198
 
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 

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>