Anonymous
Not logged in
Talk
Contributions
Create account
Log in
Publication Station
Search
Editing
Courses/Design & Technique-Essential Web Design/Q2/02
(section)
From Publication Station
Namespaces
Page
Discussion
More
More
Page actions
Read
Edit
History
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
= Javascript (JS) = ==Resources == * Code Academy JS basic course: https://www.codecademy.com/learn/javascript * JS reference https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference * '''Code Academy''' jQuery basic course: https://www.codecademy.com/en/tracks/jquery * '''Try jQuery''' http://try.jquery.com/ == what is JS?== JS is a simple programming language that runs inside webpages when the pages are loaded in the users' browser. In the complex organism that can be a web-page, you have: * HTML - content & structure * CSS - look * Javascript - (artificial) intelligence. ==You can use JS to== * change HTML content * change element attributes * change an element style * retrieve the window size * make a timer * validate data == JS in a HTML page== Javascript appears '''inside''' the html page inside the <code><script></code> tag <source lang="html4strict"> <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> </head> <body> <h1>starting javascript-ing</h1> <p id='greetings' >within the script tag</p> <script> alert('Hello Javascript!'); </script> </body> </html> </source> ==JS in a separate file== The JS is imported from another file. '''HTML file:''' <source lang="html4strict"> <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <script src="hello.js"></script> </head> <body> <h1>starting javascript-ing</h1> <p>within the script tag</p> </body> </html> </source> '''JavaScript file: hello.js''' <source lang="javascript"> alert("hello world! Greetings from javascript"); </source> == making statements == JS statements are like phrases, that do very specific tasks. Each statement has to be terminated with ";" Like the above <code>alert('Welcome to this page');</code> ==the browser console== [[File:JS-console.png|600px]] Essential to prototype, and debug your JS code. <noinclude> ==Data types== JS has essential data-types, that is types of information: numbers, strings, booleans and lists ===Numbers=== both positive or negative, integer or floating. E.g. <code>1, 0.2, -10</code> * Numbers' operations: Numbers can be added, subtracted, multiplied, and perform a number of other math operations. <code>1/3</code> ===Strings=== Essentially words or sentences. They are easy identified since they are inside quotation-marks (<code>"</code> or <code>'</code>). E.g. "I am a string", 'so am I', "900 - numbers inside quotations are also strings" * Strings can be added, <code>"string " + "is added";</code> * change to upper/lower case <code>"string".toUpperCase();</code> * asked it includes a given string <code>"string".includes("ing");</code> * asked how long is the string <code>"string".length</code> ===Booleans=== consists of simply two values: true or false <code>"I am a string".includes("I am");</code> <code>isFinite(10/0)</code> ===lists or arrays=== List or arrays are simply collections of multiple "things", which can be numbers, strings or booleans. And are always surrounded by <nowiki>[ ]</nowiki> (square brackets). <code>["apples", "bananas", 14, 0, false]</code> A list containing numbers, strings and booleans lists can be: * asked what it contains in each position <code>["apples", "bananas", 14, 0, false][0]</code> * sorted (put in order) <code>["apples", "bananas", 14, 0, false].sort()</code> or reversed <code>["apples", "bananas", 14, 0, false].reverse()</code> * added a new item <code>["apples", "bananas", 14, 0, false].push('new item')</code> * removed the last item <code>["apples", "bananas", 14, 0, false].pop()</code>, or first item <code>["apples", "bananas", 14, 0, false].shift()</code> == Variables == So far we have been using these various types of data, yet we cannot do much with them, we cannot manipulate them or reuse them, without storing them. For that we have variables, like little memory cells, that hold the information with give it to it. <source lang="javascript"> var a = 10; var b = 2; var c = a * b; var mylist = ["bananas", "apples", a]; mylist.push(c); var bananas = mylist[0]+a; </source> ==Example== A Javascript clock <source lang="html4strict"> <!DOCTYPE html> <html> <head> <style> </style> </head> <body> <h1>Time is</h1> <h2 id="time"></h2> <script> var now = new Date(); var hours = now.getHours(); var minutes = now.getMinutes(); var seconds = now.getSeconds(); console.log(hours, minutes, seconds); <!-- add date to page --> var h2 = document.getElementById('time'); h2.innerHTML=( hours.toString() + ":" + minutes.toString() + ":" + seconds.toString()); </script> </body> </html> </source> ==Resources == * Code Academy JS basic course: https://www.codecademy.com/learn/javascript * JS reference https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference == Javascript cocktail example == <source lang="html4strict"> <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> </head> <body> <h1>starting javascript-ing</h1> <p id="content">within the script tag</p> <script> var min = 0; var max = 2; var random = Math.floor(Math.random()*(max - min + 1)) + min; var bottle=["rum","gin","vodka"][random]; var herb=["mint","coriander","lime"][random]; var powder="sugar"; var cool="ice blocks"; var d = new Date(); var hour = d.getHours(); var min = d.getMinutes(); var content = document.getElementById("content"); content.innerHTML = bottle + " with " + herb + "<br/>At "+hour+" : "+min+"?" ; </script> </body> </html> </source> </noinclude>
Summary:
Please note that all contributions to Publication Station are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
Publication Station:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation
Main navigation
Main page
Printmaking Studio
Print Studio
Dig. Publishing Studio
Namespaces
Grafiwiki
Random Page
Log in
Wiki tools
Wiki tools
Page tools
Page tools
User page tools
More
What links here
Related changes
Page information
Page logs