18 JavaScript(Cont.)

download 18 JavaScript(Cont.)

of 19

Transcript of 18 JavaScript(Cont.)

  • 8/6/2019 18 JavaScript(Cont.)

    1/19

    1

    Resource Person: Muhammad Babar Nazir

  • 8/6/2019 18 JavaScript(Cont.)

    2/19

    JavaScript Objects String Object

    Date Object

    Array Object Boolean Object

    Math Object

    RegExp Object

    Browser Detection

    Form Validation

    E-mail Validation 2

    Resource Person: Muhammad Babar Nazir

  • 8/6/2019 18 JavaScript(Cont.)

    3/19

    JavaScript is an Object Oriented Programming

    (OOP) language

    There are many built-in objects available inJavaScript

    We will explore some important object in

    JavaScript

    Resource Person: Muhammad Babar Nazir

  • 8/6/2019 18 JavaScript(Cont.)

    4/19

    The String object is used to manipulate a

    stored piece of text

    There are many methods available tomanipulate strings

    var txt="Hello world!";

    document.write(txt.length);

    document.write(txt.toUpperCase());

    document.write(txt.indexOf(w));

    Resource Person: Muhammad Babar Nazir

  • 8/6/2019 18 JavaScript(Cont.)

    5/19

    The Date object is used to work with dates and

    times

    There are four types of constructors available toinstantiate Date object

    new Date() // current date and time

    new Date(milliseconds) //since 1970/01/01

    new Date(dateString)new Date(year, month, day, hours, minutes,

    seconds, milliseconds)

    Resource Person: Muhammad Babar Nazir

  • 8/6/2019 18 JavaScript(Cont.)

    6/19

    The Array object is used to store multiple values

    in a single variable

    An array is a special variable, which can holdmore than one value, at a time

    var myCars=new Array();

    myCars[0]="Saab";

    myCars[1]="Volvo";

    myCars[2]="BMW";

    Resource Person: Muhammad Babar Nazir

  • 8/6/2019 18 JavaScript(Cont.)

    7/19

    Three ways to initialize an array

    var myCars=new Array();

    var myCars=new Array("Saab","Volvo","BMW");

    var myCars=["Saab","Volvo","BMW"];

    Resource Person: Muhammad Babar Nazir

  • 8/6/2019 18 JavaScript(Cont.)

    8/19

    The Boolean object is used to convert a non-Boolean value to a Boolean value (true or false)

    To construct an object with false value

    var myBoolean=new Boolean();var myBoolean=new Boolean(0);var myBoolean=new Boolean(null);var myBoolean=new Boolean("");

    var myBoolean=new Boolean(false);var myBoolean=new Boolean(NaN);

    Resource Person: Muhammad Babar Nazir

  • 8/6/2019 18 JavaScript(Cont.)

    9/19

    To construct an object with true value

    var myBoolean=new Boolean(true);

    var myBoolean=new Boolean("true");

    var myBoolean=new Boolean("false");

    var myBoolean=new Boolean("Richard");

    Resource Person: Muhammad Babar Nazir

  • 8/6/2019 18 JavaScript(Cont.)

    10/19

    The Math object allows you to perform mathematical

    tasks

    Resource Person: Muhammad Babar Nazir

    abs(x) Returns the absolute value of x

    acos(x) Returns the arccosine of x, in radians

    asin(x) Returns the arcsine of x, in radians

    atan(x) Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians

    atan2(y,) Returns the arctangent of the quotient of its arguments

    ceil(x) Returns x, rounded upwards to the nearest integer

    cos(x) Returns the cosine of x (x is in radians)

    exp(x) Returns the value of Ex

    floor(x) Returns x, rounded downwards to the nearest integer

  • 8/6/2019 18 JavaScript(Cont.)

    11/19

    log(x) Returns the natural logarithm (base E) of x

    max(x,y,z,...,

    n)Returns the number with the highest value

    min(x,y,z,...,

    n)Returns the number with the lowest value

    pow(x,y) Returns the value of x to the power of y

    random() Returns a random number between 0 and 1

    round(x) Rounds x to the nearest integer

    sin(x) Returns the sine of x (x is in radians)

    sqrt(x) Returns the square root of x

    tan(x) Returns the tangent of an angle

    Resource Person: Muhammad Babar Nazir

  • 8/6/2019 18 JavaScript(Cont.)

    12/19

    A regular expression is an object that describesa pattern of characters

    Regular expressions are used to perform

    pattern-matching and "search-and-replace"functions on text

    var patt1=new RegExp("e");

    The RegExp Object has 3 methods: test()

    exec()

    compile()

    Resource Person: Muhammad Babar Nazir

  • 8/6/2019 18 JavaScript(Cont.)

    13/19

    The Navigator object contains information about the visitor'sbrowser

    var browser=navigator.appName;var b_version=navigator.appVersion;var version=parseFloat(b_version);

    document.write("Browser name: "+ browser);document.write("
    ");

    document.write("Browser version: "+ version);

    Resource Person: Muhammad Babar Nazir

  • 8/6/2019 18 JavaScript(Cont.)

    14/19

    function detectBrowser()

    {var browser=navigator.appName;

    var b_version=navigator.appVersion;

    var version=parseFloat(b_version);

    if ((browser=="Netscape"||browser=="Microsoft Internet Explorer")

    && (version>=4))

    {alert("Your browser is good enough!");

    }

    else

    {

    alert("It's time to upgrade your browser!");

    }

    }

  • 8/6/2019 18 JavaScript(Cont.)

    15/19

    JavaScript can be used to validate data inHTML forms before sending off the content to aserver

    Form data that typically are checked by aJavaScript could be

    has the user left required fields empty?

    has the user entered a valid e-mail address? has the user entered a valid date?

    has the user entered text in a numeric field?

    Resource Person: Muhammad Babar Nazir

  • 8/6/2019 18 JavaScript(Cont.)

    16/19

    function validate_required(field,alerttxt){if (value==null||value=="")

    {alert(alerttxt);return false;

    }else

    {return true;}

    }

    Resource Person: Muhammad Babar Nazir

  • 8/6/2019 18 JavaScript(Cont.)

    17/19

    function validate_email(field,alerttxt){with (field){apos=value.indexOf("@");dotpos=value.lastIndexOf(".");if (apos

  • 8/6/2019 18 JavaScript(Cont.)

    18/19

    JavaScript Popup Boxes

    JavaScript Functions

    JavaScript Events

    Resource Person: Muhammad Babar Nazir

  • 8/6/2019 18 JavaScript(Cont.)

    19/19

    Questions???

    Resource Person: Muhammad Babar Nazir