2 Alerts and the If/Else Conditional Statement CONTINUED There's No Right Way to Do It There are, literally, a million ways to write any given script.

Slides:



Advertisements
Similar presentations
JavaScript Chapter 7 Working with Strings. String Operations (Chap 2) var colors = red blue; var colors = red blue; var sizes = small large; var sizes.
Advertisements

Introducing JavaScript
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Computer Science & Engineering 2111 Text Functions 1CSE 2111 Lecture-Text Functions.
Java Programming Strings Chapter 7.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
Fundamental Programming Structures in Java: Strings.
Tutorial 14 Working with Forms and Regular Expressions.
XP Tutorial 1 New Perspectives on JavaScript, Comprehensive1 Introducing JavaScript Hiding Addresses from Spammers.
Introduction to JavaScript for Python Programmers
Introduction to scripting
1.
Chapter 9 Creating Formulas that Manipulate Text Microsoft Office Excel 2003.
XP Tutorial 14 New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
Tutorial 14 Working with Forms and Regular Expressions.
Scripting Languages.
Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel.
© 2000 – All Rights Reserved - Page 1 Introduction to JavaScript Programming Part Two.
Thursday, August 13 th, 2015 Instructor: Craig Duckett Basic Forms, Input/Output, Numbers, Strings, Dates.
CSC 330 E-Commerce Teacher Ahmed Mumtaz Mustehsan Ahmed Mumtaz Mustehsan GM-IT CIIT Islamabad GM-IT CIIT Islamabad CIIT Virtual Campus, CIIT COMSATS Institute.
String Object.  Conundrum: I would like people to have more time on the revisions…  … but I don't have a lot of time myself for more grading  A1 Revision.
Data types, Literals (constants) and Variables Data types specify what kind of data, such as numbers and characters, can be stored and manipulated within.
1 JavaScript in Context. Server-Side Programming.
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.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 2: What is JavaScript?
CHAPTER 4 Java Script อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
Conditional Execution
A Balanced Introduction to Computer Science, 3/E David Reed, Creighton University ©2011 Pearson Prentice Hall ISBN Chapter 15 JavaScript.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
JavaScript - Basic Concepts Prepared and Presented by Hienvinh Nguyen, Afshin Tiraie.
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
Chapter 7: Characters, Strings, and the StringBuilder.
Project 1: Using Arrays and Manipulating Strings Essentials for Design JavaScript Level Two Michael Brooks.
JavaScript, Fourth Edition
JavaScript. JavaScript is the programming language of HTML and the Web. Easy to learn One of the 3 languages of all developers MUST learn: 1. HTML to.
1 Chapter 3 – Examples The examples from chapter 3, combining the data types, variables, expressions, assignments, functions and methods with Windows controls.
1 JavaScript in Context. Server-Side Programming.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming strings.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
XP Tutorial 7 New Perspectives on JavaScript, Comprehensive 1 Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
JavaScript and Ajax (JavaScript Data Types) Week 2 Web site:
JavaScript Events. Understanding Events Events add interactivity between the web page and the user Events add interactivity between the web page and the.
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.
INTERNET APPLICATIONS CPIT405 JavaScript Instructor: Rasha AlOmari
Client-side (JavaScript) Validation. Associating a function with a click event – Part 1 Use the input tag’s onclick attribute to associate a function.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 15 JavaScript.
Introduction to Calculated Columns Variables, Conditionals, and String Manipulation PRESENTER: Cameron Blashka| Informer Implementation Specialist| April.
Session 2 Basics of PHP.
>> Introduction to JavaScript
JavaScript Objects.
Project 9 Creating Pop-up Windows, Adding Scrolling Messages, and Validating Forms.
SEEM4570 Tutorial 05: JavaScript as OOP
WEB APPLICATION PROGRAMMING
JavaScript Syntax and Semantics
JavaScript.
Week#2 Day#1 Java Script Course
Modern JavaScript Develop And Design
WEB PROGRAMMING JavaScript.
T. Jumana Abu Shmais – AOU - Riyadh
Introduction to Computer Science
JavaScript: Introduction to Scripting
CIS 136 Building Mobile Apps
Web Programming and Design
What We Want To Do User enters: Mary Smith
Presentation transcript:

2 Alerts and the If/Else Conditional Statement CONTINUED There's No Right Way to Do It There are, literally, a million ways to write any given script and still have it work correctly. For instance, braces are not required on conditionals if (and only if) there is only one statement in that code block. In addition, there’s an alternate method of writing a conditional that takes the form: (condition) ? truePart : falsePart; which is the rough equivalent of: if (condition) { truePart; } else { falsePart; } TIP In this class, for the most part and for clarity’s sake, I’ve included the braces in the examples and chosen to use the longer form for conditionals.

3 Alerts and the If/Else Conditional Statement CONTINUED There's No Right Way to Do It CONTINUED That same shorthand method can also be used to set variables; for instance: myNewVariable = (condition) ? trueValue : falseValue; is equivalent to: if (condition) { myNewVariable = trueValue; } else { myNewVariable = falseValue; } There’s also no requirement that the braces have to be at the end or beginning of lines, or that the true and false code blocks need to be indented. It’s all a matter of style, and the correct style to use is the one you’ve found to work best for you. TIP

4 Ultra Basic Form Validation with JavaScript indexOf() Method (JavaScript) The indexOf() method returns the position of the first occurrence of a specified value in a string. This method returns -1 if the value to search for never occurs. Note: The indexOf() method is case sensitive.

5 Ultra Basic Form Validation with JavaScript indexOf() Method Example (Returns a 15) Click button to locate where in string a specified value occurs. Try it function myFunction() { var str= "Rex Winkus for Supreme Leader!"; var n = str.indexOf("Supreme"); document.getElementById("demo").innerHTML = n; } Rex Winkus for Supreme Leader!

6 Ultra Basic Form Validation with JavaScript indexOf() Method Example (Returns a -1) Click button to locate where in string a specified value occurs. Try it function myFunction() { var str= "Rex Winkus for Supreme Leader!"; var n = str.indexOf("supreme"); // <-- lower case document.getElementById("demo").innerHTML = n; }

7 Ultra Basic Form Validation with JavaScript We'll continue to look at some ultra basic forms and form validation with JavaScript and jQuery:

8 Changing HTML on a Page with JavaScript

9 jQuery html() Method The html() method sets or returns the content (innerHTML) (text and HTML markup) of the selected elements. When this method is used to return content, it returns the content of the first matched element. When this method is used to set content, it overwrites the content of all matched elements. Example: Change the content of all elements: $("button").click(function(){ $("p").html("Hello world !"); }); Tip: To set or return only the text content of the selected elements, use the text() method. TIP

10 Changing HTML on a Page with JavaScript jQuery text() Method The text() method sets or returns the text content of the selected elements. When this method is used to return content, it returns the text content of all matched elements (HTML markup will be removed). When this method is used to set content, it overwrites the content of all matched elements. Example: Set the text content for all elements: $("button").click(function(){ $("p").text("Hello world!"); }); Tip: To set or return the innerHTML (text and HTML markup) of the selected elements, use the html() method. TIP

11 Changing HTML on a Page with JavaScript jQuery append() Method The append() method inserts specified content at the end of the selected elements. Example: Insert content at the end of all elements $("button").click(function(){ $("p").append(" Appended text "); }); Tip: To insert content at the beginning of the selected elements, use the prepend() method. TIP

12 Changing HTML on a Page with JavaScript jQuery prepend() Method The prepend() method inserts specified content at the beginning of the selected elements. Example: Insert content at the beginning of all elements $("button").click(function(){ $("p").prepend(" Prepended text "); }); Tip: To insert content at the end of the selected elements, use the append() method. TIP

13 Numbers, Strings, and Dates

14 Numbers, Strings, and Dates JavaScript eval() Function The eval() function evaluates or executes an argument. If the argument is an expression even if it is a string, eval() evaluates the expression. If the argument is one or more JavaScript statements, eval() executes the statements. Example: evaluate/execute expressions The result of result would be: var x = 10; var y = 20; var a = eval("x * y"); 200 var b = eval(" "); 15 var c = eval("x + y + 17"); 47 Special Note The eval() function can be successfully used to complete Assignment 1 (The Basic Calculator)

15 Numbers, Strings, and Dates JavaScript toFixed() Method The toFixed() method converts a number into a string, keeping a specified number of decimals. Example: Convert a number into a string, keeping only two decimals: var num = ; var n=num.toFixed(2); The result of n would be: 3.14 Note: if the desired number of decimals are higher than the actual number, nulls/zeros are added to create the desired decimal length. TIP

16 Numbers, Strings, and Dates JavaScript String substr() Method The substr() method extracts parts of a string, beginning at the character at the specified position, and returns the specified number of characters. Example: Extract parts of a string var str = "Hello world!"; var res = str.substr(4,4) The result of res would be: o wo Tip: To extract characters from the end of the string, use a negative start number (This does not work in IE8 and earlier) Note: The substr() method does not change the original string. TIP

17 Numbers, Strings, and Dates JavaScript String toLowerCase() Method The toLowerCase() method converts a string to lowercase letters. Example: Convert the string to lowercase letters var str = "Hello World!"; var res = str.toLowerCase(); The result of res would be: hello world! Note: The toLowerCase() method does not change the original string. Tip: Use the toUpperCase() method to convert a string to uppercase letters. TIP

18 Numbers, Strings, and Dates JavaScript String toUpperCase() Method The toUpperCase() method converts a string to uppercase letters. Example: Convert the string to uppercase letters var str = "Hello World!"; var res = str.toUpperCase() The result of res would be: HELLO WORLD! Note: The toUpperCase() method does not change the original string. Tip: Use the toLowerCase() method to convert a string to lowercase letters. TIP

19 Numbers, Strings, and Dates JavaScript toDateString() Method The toDateString() method converts the date (not the time) of a Date object into a readable string. Example: Convert today's date into a readable string var d = new Date(); var n = d.toDateString(); The result of n would be: Mon Jan ! Example: The Date() Object without the conversion: var d = new Date(); document.write(d); The result would be: Mon Jan :05:33 GMT-0800 (Pacific Standard Time) TIP

20 Numbers, Strings, and Dates Some Methods of the Date Object: getMonth() The getMonth() method returns the month (from 0 to 11) for the specified date, according to local time. Example: Return the Current Month var d = new Date(); var n = d.getMonth(); The result of n would be 0 January is 0, February is 1, March is 2, and so on. TIP

21 Numbers, Strings, and Dates Some Methods of the Date Object: getDate() The getDate() method returns the day of the month Example: Return the day of the month var d = new Date(); var n = d.getDate(); The result of n would be 27 The days of the month start with 1 instead of 0. TIP

22 Numbers, Strings, and Dates JavaScript toDateString() Method The toDateString() method converts the date (not the time) of a Date object into a readable string. Example: Convert today's date into a readable string var d = new Date(); var n = d.toDateString(); The result of res would be: Mon Jan !

23 Assorted Libraries Custom Dialog Boxes Custom Dialog Boxes Date JS Date JS Pretty Date Pretty Date