<!doctype html>
<meta charset= "UTF-8" >
<meta name= "Generator" content= "EditPlus?" >
<title>index </title>
<script type= "Text/javascript" src= "Results.js"/></script>
<link rel= "Result.css" type= "Text/css"/>
<body>
<p id= "Datefield" > </p>
</body>
*******************************************************************************
Date object:
var mydate=new Date ();
var datestring=mydate.tolocaledatestring () + "" +mydate.tolocaletimestring ();
var Dateloc=document.getelementbyid ("Datefield");
Dateloc.innerhtml= "Hello-page Rendered on" +datestring;
*******************************************************************************
Countdown to one day in the future:
var today=new Date ();
var then=new Date ();
Then.setfullyear (2012,0,1);
var diff=then.gettime ()-today.gettime (); GetTime () is given in milliseconds.
Diff=math.floor (diff/(1000*60*60*24));
var Dateloc=document.getelementbyid ("datefiled");
Dateloc.innerhtml= "There is" +diff+ "days until 1/1/2012";
*******************************************************************************
Calculate Render Time:
var started=new Date ();
var now=started.gettime ();
var bottom=new Date ();
var diff= (Bottom.gettime ()-now)/1000;
var finaltime=diff.toprecision (5);
var Dateloc=document.getelementbyid ("datefiled");
Dateloc.innerhtml= "page rendered in" +finaltime+ "seconds.";
Delay:
for (Var i=0;i<100000;i++) {
Delay
}
*******************************************************************************
Regular Expressions:
var mystring= "http://www.braingia.org";
var myregex=/http:\/\/\w+\. (.*)/;
var results=myregex.exec (myString);
Alert (results[1]);
Match Method:
var emailaddr= "[email protected]";
var myregex=/\.com/;
var checkmatch=emailaddr.match (Myregex);
Alert (checkmatch[0]);
Determines whether a given email address contains a string . com:
var emailaddr= "[email protected]";
var myregex=/\.com/;
var checkmatch=emailaddr.match (Myregex);
if (checkmatch!==null) {
Alert (checkmatch[0]);
}
Search Method:( Returns the index value (location) of the first match)
var emailaddr= "[email protected]";
var myregex=/\.com/;
var searchresult=emailaddr.search (Myregex);
Alert (checkmatch[0]);
Replace method:
var emailaddr= "[email protected]";
var myregex=/\.com$/;
var replacewith= ". Net";
var result=emailaddr.match (Myregex);
alert (result);
*******************************************************************************
Using the detele operator:
var star={};
star["Polaris"]=new object;
star["Mizar"]=new object;
star["Aldebaran"]=new object;
star["Rigel"]=new object;
star["Polaris"].constellation= "Ursa Minor";
star["Mizar"].constellation= "Ursa Minor";
star["Aldebaran"].constellation= "Ursa Minor";
star["Rigel"].constellation= "Ursa Minor";
Detele (star["Polaris"].constellation);
For (Starname in Star) {
var para=document.createelement (' P ');
Para.id=starname;
Para.appendchild (document.createTextNode (starname+ ":" +star[starname].constellation));
document.getElementsByTagName ("Body") [0].appendchild (para);
}
*******************************************************************************
For in Loop:
for (Var myprop in MyObject) {
Alert (myprop+ "=" +myobject[myprop]);
}
Var star=new object;
Star.name= "";
Star.type= "";
Star.constellation= "";
for (Var starprop in Star) {
Alert (starprop+ "=" +star[starprop]);
}
*******************************************************************************
Form validation:
<!doctype html>
<meta charset= "UTF-8" >
<meta name= "Generator" content= "editplus®" >
<title> Sentiment analysis results </title>
<script type= "Text/javascript" src= "Results.js"/></script>
<link rel= "Result.css" type= "Text/css"/>
<body>
<form id= "Myfrom" action= "#" onsubmit= "return Alertname ();" >
<p>username:<input id= "Nametext" name= "Username" type= "text"/></p>
<p><input type= "Submit"/></p>
</form>
</body>
function Alertname () {
var Name=document.forms[0].nametext.value;
if (name== "Steve") {
Alert ("Hello Steve. Welcome to Machine ");
}
else if (name== "Nancy") {
Alert ("Hello Tim.");
}
else{
Alert ("Hello" +name);
}
return true;
}
*******************************************************************************
W Some properties of the Indow object:
Close: set to True when the window is closed ;
Defaultstatus: Used to set the default text that appears in the browser status bar;
Name: The window that is set when the window is opened for the first time;
Opener: Refers to the window that created the window;
Parent: often used in conjunction with frames, a window that creates a particular window, or the upper-level window of that frame;
Status: Often used to set the text in the status bar when a visitor hovers over an element such as a link;
Top: Parent window of the highest or topmost level;
*******************************************************************************
Some methods of the Window object:
AddEventListener (): cross-browser (except Internet Explorer) event handler method.
Attachevent (): cross-browser Internet Explorer event handler method.
Blur (): Move the keyboard input focus away from the browser window.
Focus (): moves the keyboard input focus to the browser window.
Close (): closes the browser window.
DetachEvent (): cross-browser Internet Explorer event handler removal method.
RemoveEventListener (): cross-browser (except Internet Explorer) event handler removal method.
Open (): opens a window.
Print (): causes the printer function to invoke the browser, which behaves as if someone clicked the browser's print button.
*******************************************************************************
W Indow How to move and resize a Window object:
Moveby (): used to move the window to a relative position
MoveTo (): used to move the window to a specific location
Resizeby (): used to change the window size by a relative amount
Resizeto (): used to change the window size to a specific size
*******************************************************************************
W Timer-related methods in the Indow object:
Clearinterval ():
Cleartimeout ():
SetInterval ():
SetTimeout ():
*******************************************************************************
Get information about the screen:
Availheight/availwidth ( usable height )/colordepth/height/width (total height)
E.g:alert (Screen.availheight);
*******************************************************************************
Get element:
getElementById () and getElementsByTagName ()
HTML Collection:
Document.anchors
Document.forms
Document.images
Document.links
*******************************************************************************
Window Size:
var w=window.innerwidth
|| Document.documentElement.clientWidth
|| Document.body.clientWidth;
var h=window.innerheight
|| Document.documentElement.clientHeight
|| Document.body.clientHeight;
JavaScript knowledge points