Examples javascript

Discussion in 'JavaScript' started by youness_violon, Oct 6, 2008.

  1. #1
    Create a welcome cookie

    <html>
    <head>
    <script type="text/javascript">
    function getCookie(c_name)
    {
    if (document.cookie.length>0)
    {
    c_start=document.cookie.indexOf(c_name + "=");
    if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1 ;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length
    return unescape(document.cookie.substring(c_start,c_end));
    }
    }
    return ""
    }

    function setCookie(c_name,value,expiredays)
    {
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : "; expires="+exdate.toGMTString());
    }

    function checkCookie()
    {
    username=getCookie('username');
    if (username!=null && username!="")
    {
    alert('Welcome again '+username+'!');
    }
    else
    {
    username=prompt('Please enter your name:',"");
    if (username!=null && username!="")
    {
    setCookie('username',username,365);
    }
    }
    }
    </script>
    </head>
    <body onLoad="checkCookie()">
    </body>
    </html>


    A clock created with a timing event

    <html>
    <head>
    <script type="text/javascript">
    function startTime()
    {
    var today=new Date();
    var h=today.getHours();
    var m=today.getMinutes();
    var s=today.getSeconds();
    // add a zero in front of numbers<10
    m=checkTime(m);
    s=checkTime(s);
    document.getElementById('txt').innerHTML=h+":"+m+":"+s;
    t=setTimeout('startTime()',500);
    }

    function checkTime(i)
    {
    if (i<10)
    {
    i="0" + i;
    }
    return i;
    }
    </script>
    </head>

    <body onload="startTime()">
    <div id="txt"></div>
    </body>
    </html>





    Style strings

    <html>
    <body>

    <script type="text/javascript">

    var txt="Hello World!";

    document.write("<p>Big: " + txt.big() + "</p>");
    document.write("<p>Small: " + txt.small() + "</p>");

    document.write("<p>Bold: " + txt.bold() + "</p>");
    document.write("<p>Italic: " + txt.italics() + "</p>");

    document.write("<p>Blink: " + txt.blink() + " (does not work in IE)</p>");
    document.write("<p>Fixed: " + txt.fixed() + "</p>");
    document.write("<p>Strike: " + txt.strike() + "</p>");

    document.write("<p>Fontcolor: " + txt.fontcolor("Red") + "</p>");
    document.write("<p>Fontsize: " + txt.fontsize(16) + "</p>");

    document.write("<p>Lowercase: " + txt.toLowerCase() + "</p>");
    document.write("<p>Uppercase: " + txt.toUpperCase() + "</p>");

    document.write("<p>Subscript: " + txt.sub() + "</p>");
    document.write("<p>Superscript: " + txt.sup() + "</p>");

    document.write("<p>Link: " + txt.link("http://www.w3schools.com") + "</p>");
    </script>

    </body>
    </html>


     
    youness_violon, Oct 6, 2008 IP