Introduction to JavaScript for Python Programmers

Slides:



Advertisements
Similar presentations
Java Script Session1 INTRODUCTION.
Advertisements

The Web Warrior Guide to Web Design Technologies
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
Computer Science 103 Chapter 3 Introduction to JavaScript.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
Introduction to Web Site Development Steven Emory Department of Computer Science California State University, Los Angeles Lecture 8: JavaScript I.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
2012 •••••••••••••••••••••••••••••••••• Summer WorkShop Mostafa Badr
Introduction to scripting
Python quick start guide
Javascript and the Web Whys and Hows of Javascript.
4.1 JavaScript Introduction
Scripting Languages.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
SYST Web Technologies SYST Web Technologies Lesson 6 – Intro to JavaScript.
Arrays – What is it? – Creation – Changing the contents Functions – What is it? – Syntax – How they work – Anonymous functions A quick lesson in Objects.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
What is Java Script? An extension to HTML. An extension to HTML. Allows authors to incorporate some functionality in their web pages. (without using CGI.
Javascript. Outline Introduction Fundamental of JavaScript Javascript events management DOM and Dynamic HTML (DHTML)
Chapter 4 JavaScript and Dynamic Web pages. Objectives Static Web pages Dynamic Web pages JavaScript Variables Assignments. JavaScript Functions –(prompt(“”,””)
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Client Side Programming with JavaScript Why use client side programming? Web sides built on CGI programs can rapidly become overly complicated to maintain,
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
Using Client-Side Scripts to Enhance Web Applications 1.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
Client-Side Scripting JavaScript.  produced by Netscape for use within HTML Web pages.  built into all the major modern browsers. properties  lightweight,
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
ALBERT WAVERING BOBBY SENG. Week 4: JavaScript  Quiz  Announcements/questions.
PHP Constructs Advance Database Management Systems Lab no.3.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
L OO P S While writing a program, there may be a situation when you need to perform some action over and over again. In such situation you would need.
XP Tutorial 10New Perspectives on HTML and XHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial.
1 JavaScript
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
JavaScript, Fourth Edition
COMP403 Web Design JAVA SCRİPTS Tolgay KARANFİLLER.
1 Chapter 3 – JavaScript Outline Introduction Flowcharts Control Structures if Selection Structure if/else Selection Structure while Repetition Structure.
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Pertemuan 5 IT133 Pengembangan Web JavaScript. What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
JavaScript Introduction and Background. 2 Web languages Three formal languages HTML JavaScript CSS Three different tasks Document description Client-side.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
JavaScript, Sixth Edition
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
Javascript Basic Concepts Presentation By: Er. Sunny Chanday Lecturer CSE/IT RBIENT.
CGS 3066: Web Programming and Design Spring 2016 Introduction to JavaScript.
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
JavaScript Tutorial. What is JavaScript JavaScript is the programming language of HTML and the Web Programming makes computers do what you want them to.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Web Programming Java Script-Introduction. What is Javascript? JavaScript is a scripting language using for the Web. JavaScript is a programming language.
CHAPTER 10 JAVA SCRIPT.
Web Systems & Technologies
Chapter 6 JavaScript: Introduction to Scripting
Introduction to Scripting
JavaScript Syntax and Semantics
Introduction to JavaScript for Python Programmers
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
Training & Development
JavaScript: Introduction to Scripting
Javascript Chapter 19 and 20 5/3/2019.
Introduction to JavaScript
Web Programming and Design
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Presentation transcript:

Introduction to JavaScript for Python Programmers

Lecture Outline How Python & JavaScript are similar How Python & Javascript are different JavaScript fundamentals JavaScript Examples

Python & Javascript: Similarities Both are programming languages Used by programmers to tell the computer what to do. Both are interpreted languages JavaScript is interpreted by the web browser, Python is interpreted by IDLE or the python interpreter. Both used to implement algorithms Series of actions and calculations, that use decisions and loops to control the flow of execution. Both are imperative languages

Python & Javascript: Differences Different web browsers support different JavaScript code. The basic syntax works on all browsers, but some browsers support non-standard extensions that are not supported on other browsers. The source code can be stored inside of a web page, sometimes intermixed with HTML. Web browsers typically do not provide good debugging information about why a JavaScript fails, so debugging a non-working script can be difficult. The Syntax of Python & Javascript are different Statements can be terminated with semi-colons. Brackets must be used to indicate blocks.

Javascript in a Web Page JavaScript is separated from HTML by putting it inside of special script tags: <script laguage=javascript> JAVASCRIPT JAVASCRIPT </script> Some javascript is actually part of HTML, for example, a function call on a button press: onClick=”cwCalc();” Output from a JavaScript program usually is shown to the user as HTML that is rendered by the web browser.

Javascript: Syntax Differences Indentation does NOT indicate blocks. Instead, curly brackets are used to indicate blocks. (Like C and C++)‏ Python: def myFunc(aVar): if (aVar == “test”): return( 0) else: return(-1) JavaScript: function myFunc(aVar) { if (aVar == 'test') { return(0) } else { return (-1) } }

Javascript: Indentation does NOT matter! This: function myFunc(aVar) { if (aVar == 'test') { return(0) } else { return (-1) } } functions the same as this: function myFunc(aVar) { if (aVar == 'test') { return(0) } else { return (-1) } }

But please use good indentation! Improves readability and understandability function myFunc(aVar) { if (aVar == 'test') { return(0) } else { return (-1) } }

Calling Functions, & Returning Values The previous example should make it clear that calling functions and returning values works basically the same in JavaScript as it does in Python. function myFunc(aVar) { if (aVar == 'test') { return(0) } else { return (-1) } } myfunc(“aString”)‏

Declaring Variables and Arrays Variables must be declared before being used: var VARIABLENAME = VALUE var myName = “Jay” The “VAR” keyword tells the JavaScript interpreter that you are making a variable. Arrays must have their size declared at time of creation: var ARRAYNAME = new Array(SIZE) var myArray = new Array(50) You index elements in an array using bracket notation: var oneElement = myArray[1]

Conditional (IF) Statements Very similar in Python & JavaScript. IF and IF/ELSE are the same (except for brackets). No ELIF statement. “Weird Conditionals”: I won't talk about them, if you run into them, do a quick web search to figure out how they work. Switch/case/break ?:

Looping (while) statement Very similar in Python & JavaScript. while (BOOLEAN EXPRESSION) { STATEMENT; STATEMENT; } New do/while statement. Test is performed at the end of the loop instead of the beginning: do { STATEMENT; STATEMENT; } while (BOOLEAN EXPRESSION)

Looping (for) statement FOR loops in JavaScript are based on C and C++ syntax, and are usually quite different than in Python. for( INITIALIZE; BOOLEAN EXPRESSION; ACTION) { STATEMENT; STATEMENT; } for (var j = 0; j < 5; j = j+1) { document.write(“j is: “ + j) }

Looping (for) statement FOR loops in JavaScript are based on C and C++ syntax, and are usually quite different than in Python. for( INITIALIZE; BOOLEAN EXPRESSION; ACTION) { STATEMENT; } Code before the first semicolon (the initialization code) is executed once at the beginning of the loop. The boolean expression (in the middle) is evaluated every time the loop repeats, and the statements in the loop are only executed if the result is TRUE. The action code is executed after the loop executes, and is typically used to increment a counter.

Looping (for) Statement Example for (var j = 0; j < 5; j = j+1) { document.write(“j is: “ + j) } The document.write statement executes 5 times, with j equalling 0,1,2,3, and 4. After the 5th execution, the j=j+1 action executes, and then the j < 5 test evaluates to FALSE, so document.write() does not execute when j = 5.

Looping (for) Statement – Iteration over Properties JavaScript does have a FOR/IN version of the for loop, but it doesn't work exactly as you'd expect from your experience with Python's FOR loops. for (VARNAME in OBJECT) { document.write( VARNAME ) document.write( OBJECT[VARNAME] ) } Note that it iterates through the property names of an object, and you have to use bracket notation to get the property values.

Input / Output with JavaScript As most JavaScript runs inside of a web browser, most of the input and output to and from a JavaScript program goes through the web-browser. Input comes from the user via web forms, button and link clicks. Output is typically is displayed as a webpage or part of a webpage as HTML rendered by the web browser.

Webpage Output with JavaScript A script can use the document.write( “a string”) function to add content to a web page as it is loaded. <html> <body> <h1>Example:</h1> <script language=javascript> document.write(“<h2>Here is my website!</h2>”) </script> </body> </html> Note that this javascript code will be executed when it is encountered, and it outputs HTML that is rendered by the web browser.

Webpage Output with JavaScript A script can output calculated and dynamic information: <html> <body> <h1>Clock Example:</h1> <script language=javascript> var date = new Date() var hours = date.getHours() var min = date.getMinutes() document.write(“Time: <b>” +hours+”:”+min+”</b>”) </script> </body> </html>

Dialog Output with JavaScript A script can use the alert( “a string”) function to pop up an “Alert” Dialog box. <html> <body> <h1>Example:</h1> <script language=javascript> alert(“Here is an annoying pop-up!”) </script> </body> </html> This javascript code will be executed when it is encountered, and causes an alert-box to appear.

Input: Running a function when the user clicks Define a function: <script language=javascript> function myFunc() { alert(“You clicked it!”) } </script> You can use an onClick=”FUNCTIONNAME();” parameter on a button to call the function: <input type=button value=”Do It!” onClick=”myFunc();” > Clicking on the button will cause the alert box to appear.

Input: Running a function when the user clicks <html> <body> <script language=javascript> function myFunc() { alert("You clicked it!")‏ } </script> <input type=button value="Do It!" onClick="myFunc();" > </body> </html> Input: Running a function when the user clicks

Output: Modifying an already loaded webpage document.write() can be used to add text to a webpage as it is being loaded. To modify text in a webpage AFTER it is loaded, you can use a named area (DIV) and replace the HTML content with javascript. <div id="txt">Plain Text</div> is HTML that creates a DIV named “txt”. You can then change the HTML content of this DIV (currently “Plain Text”) with javascript as follows: var myDIV = document.getElementById('txt') myDIV.innerHTML=”<b>New Text!</b>”

Putting it all together: An example program <html> <body> <script type="text/javascript"> var num = 0 function startTime() { num = num + 1 var myDiv = document.getElementById('txt')‏ myDiv.innerHTML="<b>Clicked " +num+ " Times</b>"; } </script> <div id="txt">Plain Text</div> <input type=button value="click it!" onClick="startTime()"> </body> </html>