Java Script Objects Class 4

download Java Script Objects Class 4

of 26

Transcript of Java Script Objects Class 4

  • 7/31/2019 Java Script Objects Class 4

    1/26

    JAVA SCRIPT OBJECTS(CONTD..)

  • 7/31/2019 Java Script Objects Class 4

    2/26

    JavaScript Browser Detection

    The Navigator object contains information

    about the visitor's browser, such as visitor's

    browser name, version, and more.

    appNameholds the name of the browser

    appVersionholds, among other things, the

    version of the browser. To pull the version number

    out of the string, we are using a function called

    parseFloat(), which pulls the first thing that looks likea decimal number out of a string and returns it.

  • 7/31/2019 Java Script Objects Class 4

    3/26

    EX 1:

    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);

  • 7/31/2019 Java Script Objects Class 4

    4/26

    EX 2:

    var x = navigator;

    document.write("CodeName=" + x.appCodeName);

    document.write("
    ");

    document.write("MinorVersion=" + x.appMinorVersion);

    document.write("
    ");

    document.write("Name=" + x.appName);

    document.write("
    ");

    document.write("Version=" + x.appVersion);

    document.write("
    ");

    document.write("CookieEnabled=" + x.cookieEnabled);document.write("
    ");

    document.write("CPUClass=" + x.cpuClass);

    document.write("
    ");

  • 7/31/2019 Java Script Objects Class 4

    5/26

    document.write("OnLine=" + x.onLine);

    document.write("
    ");

    document.write("Platform=" + x.platform);

    document.write("
    ");

    document.write("UA=" + x.userAgent);

    document.write("
    ");

    document.write("BrowserLanguage=" + x.browserLanguage);

    document.write("
    ");

    document.write("SystemLanguage=" + x.systemLanguage);

    document.write("
    ");

    document.write("UserLanguage=" + x.userLanguage);

  • 7/31/2019 Java Script Objects Class 4

    6/26

    JavaScript Form Validation

    Required Fields: The following functionchecks whether a required field has been left

    empty. If the required field is blank, an alert is

    displayed, and the function returns false. If avalue is entered, the function returns true

    (means that data is OK):

  • 7/31/2019 Java Script Objects Class 4

    7/26

    function validate_required(field,alerttxt)

    {

    with (field)

    {

    if (value==null||value==)

    {

    alert(alerttxt);return false; }else

    {

    return true; } } }

  • 7/31/2019 Java Script Objects Class 4

    8/26

    function validate_form(thisform)

    {

    with (thisform)

    {

    if (validate_required(email,Email must be filledout!)==false)

    {email.focus();return false;}

    }

    }

  • 7/31/2019 Java Script Objects Class 4

    9/26

    Email:

  • 7/31/2019 Java Script Objects Class 4

    10/26

    function validateForm()

    {

    var x=document.forms["myForm"]["fname"].value;

    if (x==null || x=="")

    {

    alert("First name must be filled out");return false;

    }

    }

  • 7/31/2019 Java Script Objects Class 4

    11/26

    First name:

  • 7/31/2019 Java Script Objects Class 4

    12/26

    E-mail Validation

    function validate_email(field,alerttxt)

    {

    with (field)

    {

    apos=value.indexOf(@);

    dotpos=value.lastIndexOf(.);

    if (apos

  • 7/31/2019 Java Script Objects Class 4

    13/26

    function validate_form(thisform)

    {

    with (thisform)

    {

    if (validate_email(email,Not a valid e-mail

    address!)==false)

    {email.focus();return false;}

    }

    }

  • 7/31/2019 Java Script Objects Class 4

    14/26

    Email:

  • 7/31/2019 Java Script Objects Class 4

    15/26

    function validateForm()

    {var x=document.forms["myForm"]["email"].value;

    var atpos=x.indexOf("@");

    var dotpos=x.lastIndexOf(".");

    if (atpos

  • 7/31/2019 Java Script Objects Class 4

    16/26

    Email:

  • 7/31/2019 Java Script Objects Class 4

    17/26

    JavaScript Timing Events

    setTimeout()Executes a code some time in thefuture

    clearTimeout()Cancels the setTimeout()

    The setTimeout() and clearTimeout() are both

    methods of the HTML DOM Window object. setInterval() - executes a function, over and over

    again, at specified time intervals.

    The clearInterval() method is used to stop further

    executions of the function specified in thesetInterval() method.

    myVar=setInterval("javascriptfunction",milliseconds);

    clearInterval(intervalVariable);

  • 7/31/2019 Java Script Objects Class 4

    18/26

    function timedMsg()

    {

    var t=setTimeout("alert('I am displayed after 3 seconds!')",3000);}

  • 7/31/2019 Java Script Objects Class 4

    19/26

    var c=0;

    var t;var timer_is_on=0;

    function timedCount()

    {

    document.getElementById('txt').value=c;

    c=c+1;

    t=setTimeout("timedCount()",1000);

    }

  • 7/31/2019 Java Script Objects Class 4

    20/26

    function doTimer()

    {

    if (!timer_is_on)

    {

    timer_is_on=1;

    timedCount();

    }

    }

  • 7/31/2019 Java Script Objects Class 4

    21/26

    Click on the button above. The input field willcount forever, starting at 0.

  • 7/31/2019 Java Script Objects Class 4

    22/26

    Clearing Time out value

    The syntax is as follows: clearTimeout(setTimeout_variable)

    Ex:

    var c=0;

    var t;

    var timer_is_on=0;

    function timedCount()

    {document.getElementById('txt').value=c;

    c=c+1;

    t=setTimeout("timedCount()",1000);

    }

  • 7/31/2019 Java Script Objects Class 4

    23/26

    function doTimer(){

    if (!timer_is_on)

    {

    timer_is_on=1;

    timedCount();}

    }

    function stopCount()

    {

    clearTimeout(t);timer_is_on=0;

    }

  • 7/31/2019 Java Script Objects Class 4

    24/26

    Click on the "Start count!" button above to start the timer. Theinput field will count forever, starting at 0. Click on the "Stop

    count!" button to stop the counting. Click on the "Start count!"button to start the timer again.

  • 7/31/2019 Java Script Objects Class 4

    25/26

    Click the first button to display the current time, and thesecond button to stop the time.

    Try itStop time

    var myVar;

    function myFunction()

    {

    myVar=setInterval(function(){myTimer()},1000);

    }

  • 7/31/2019 Java Script Objects Class 4

    26/26

    function myTimer()

    {

    var d=new Date();

    var t=d.toLocaleTimeString();

    document.getElementById("demo").innerHTML=t;

    }function myStopFunction()

    {

    clearInterval(myVar);

    }