Introduction to JavaScript

  • VBScript is another scripting language that uses Visual Basic code for writing scripts.
  • ECMA approved JavaScript as an international standard in 1997.
  • The Microsoft version of JavaScript is known as JScript.
  • JavaScript is the first scripting language used for a browser.
  • The old name of JavaScript is LiveScript.
  • CGI was used to validate forms when JavaScript was not developed.
  • JavaScript is the default scripting language of Netscape Navigator.
  • If the language attribute is not defined, Netscape Navigator uses JavaScript as the default scripting language.
  • Netscape develops the JavaScript language.
  • A property is an attribute of an object used to change the style of the object.
  • Every property has a value that changes the style of an object.
  • JavaScript is interpreted at runtime. The script is not compiled.
  • The comment tag is used to hide the script from the non-scriptable browsers.
  • The <script>…<script> tag is used to insert scripts in a Web page.
  • An HTML document can contain any number of <script>…<script> tag
  • A variable declared inside the <script>…<script> tag but outside any function is known as global variable and can be accessed from any function within the <script tag>.
  • The increment (++) operator is used to increment the value of a variable by one.
  • Instantiation is a process of creating copies of different objects.

  • The *= operator is used to multiply the value of left hand side variable with the right hand side variable and store the product in the left hand side variable.
  • Concatenation is a process of combining two or more strings and converting them into a single string.
  • The != operator is used to check the inequality for a given comparison.
  • The add by value operator(+=) is used to add the value of a left hand side variable with the right hand side variable. The sum is stored in the left hand side variable.
  • The comparison operators are used to compare the values of two or more variables.
  • The logical “”and”” is used to compare two or more statements. If all the statements are true, the statement within the if block is executed; otherwise, the statement following the if block is executed.
  • The || operator does not execute if the two given conditions are false.
  • The equal to (=) operator is used to assign a value to a variable.
  • Comparison operators are used to compare two or more expressions.
  • The modulo operator is used to find out the remainder after a division has been completed.
  • The comparison operators return boolean values.
  • The typeof operator is used to retrieve the type of data assigned to a variable.
  • The delete operator removes the elements from an array.
  • The operator precedence decides which operator should be used first in a complex statement.
  • The operator precedence is an arrangement of operators in JavaScript. It decides the priority of the operators in a complex statement.
  • The not equal to operator is a comparison operator. The sign for this operator is !=. This operator is used to test the inequality of two operands and the result value is boolean.
  • The external script file is saved by using the .js extension.
  • The /*…*/ is used to give multiline comments in a script.
  • Inline scripting is used to write a script in a file and save it with the .js extension. The file can be accessed in an HTML document by using the src attribute of the <script> tag.
  • Function can be created by using the function keyword followed by a function and parenthesis.
  • The function block starts with the function keyword.
  • A constructor can be created by using the new operator. There is no such keyword as constructor.
  • JavaScript keywords are case-sensitive.
  • There is no such keyword as method in JavaScript. JavaScript objects include methods that are used to perform an action on the objects.

Functions, Methods, and Events

  • A function created by a user is known as user-defined function.
  • Arguments are passed within the parenthesis of a function. The function can return values to the calling function through the arguments.
  • Events are used to trigger a function or method invoked on an object.
  • The functions are invoked on an object by the event handlers associated with the object.
  • The onload event cannot be fired by using a mouse.
  • In JavaScript, functions and methods can be used interchangeably.
  • The parseFloat() method is used to convert a number in string to a floating-point decimal equivalent.
  • The parseInt() and parseFloat() functions are used to convert a number in string format to a numeric format.
  • The isNaN() method is used to test whether or not an entry is a number. The value returned by this method is boolean. The isNaN() method returns true if the value is not a number.
  • The escape function used to convert the ASCII value of a non-alphanumeric character to its equivalent hexadecimal value preceded by a percent sign.

Controlling Program Flow

  • The prompt dialog box is used to take input from a user and gives results as per the given code.
  • The if statements returns a boolean value.
  • The else statement cannot be used without the if statement.
  • The conditional or ternary operator is used in the same way as the if statement.
  • The while loop checks the condition before the loop starts. The do-while loop runs at least once before checking the condition.
  • The for loop is used to execute a single statement several times.
  • The break statement is used to terminate a loop in the middle.
  • The continue keyword is used to continue a loop for a given condition.
  • The switch…case statement is used to compare the values and produce the result if a condition is true.
  • The with statement is used to reduce the length of the object references.

The JavaScript Object Model

  • The dot notation is used to associate a property or a method with an object.
  • The built-in properties and methods of an object are accessed by a dot.
  • The hierarchy for accessing a form element is as follows:

    window.document.form.formelement

  • The object name must start with an alphabet or an underscore. The name should not contain spaces.
  • The location object is a subordinate of the window object.
  • The top keyword is used to access all the frames in a frameset.
  • The opener property of the window object is used to access a child window from a parent window.
  • The alert, prompt, and confirm dialog boxes belong to the window object.
  • A frame can access the properties and functions of second frame through the parent object.
  • The prompt dialog box can be used to take input from a user.
  • Different components of a browser window can be displayed or hidden by using the window.open() method.
  • The confirm dialog box contains the OK and Cancel button.
  • The onload event handler is used with the <body>tag.
  • The closed property of the window object is used to check whether or not a child window is closed.
  • The event handlers of the window object are used in the <body>tag.
  • A window can be closed by using the following expressions:
    • window.close()
    • self.close()
    • close()
  • The full reference of the document.write() method is window.document.write.
  • To write a text dynamically from a frame to another frame, following syntaxes are used:
    • top.frame2.document.write()
    • parent.frame2.document.write()
  • A window object has only one document object.
  • If there are two frames in a window named frame1 and frame2, frame1 can access frame2 by using the following code:
    • parent.frames[1].document.title
    • parent.frames[“”frame2″”].document.title
  • The name property of the frame object is used to retrieve the name of a frame in a code.
  • The onerror property belongs to the window object. It is used to close the error dialog box when a document is being loaded.
  • The following code is used to close an error window:

    window.onerror=null

  • The prompt function is used to take input from a user.
  • The onblur event is fired when the object on which it is invoked loses the focus.
  • The setInterval() method is used to run a set of statements at regular interval.
  • The setTimeout() method is used to fix a time in the future for the execution of a function or expression.
  • The URL of a document in a frame can be retrieved by using the following code:
    parent.frames[0].location
    parent.frame1.location
  • When the window.home property is used, a page is displayed whose URL is mentioned in the browser preferences.
  • The window.open method can be used without its arguments.
  • The defaultStatus property is used to put a default text at the statusbar of a browser window.
  • The close() method belongs to the window object. This method is used to close a window when an event is fired.
  • The focus() method is used to activate a window. A window gains the focus when the focus() method is invoked on it.
  • The window.open method is used to open a new window from an existing window.
  • The syntax of using the open method is as follows:
    window.open(“”URL””, “”windowname””, “”windowfeatures””)
  • The confirm method belongs to the window object. It is used to present a message in a modal dialog box along with the OK and Cancel buttons.
  • A text can be made colorful by using the fgColor property of the document object.
  • The cookie property of the document object is used to store small pieces of text on client computers.
  • The anchors property belongs to the document object.
  • The following code is used to change the color of an active link in a window:
    document.alinkColor=””#AABBCC””
  • The window.open method creates a window object and loads a document, while the document.open method uses the document.write method to insert a content.
  • The window.open method opens a new window and loads the document, while the document.open method inserts the content in the same window.
  • A link object can be accessed by using the following syntaxes:
    • document.linkname
    • document.links[linkindex]
  • The form and the links object are the subordinate of the document object.
  • JavaScript contains some objects as arrays such as forms, links, anchors, etc.
  • The <a> tag is used to create link objects.
  • The anchor object is created by using the<a> tag.
  • The document object is created by using the <body>tag.
  • When the secure attribute of the cookie object is set, the cookies can be transmitted only through the https protocol
  • .

  • The lastModified property is used to get the date when the page was previously modified.
  • The fgColor property and the fontcolor property are used to change the color of a text in a document. The fgColor property changes the color of all the text in a document whereas the fontcolor property can be used to change the color of a small portion of text.
  • The alinkColor property belongs to the document object. This property is used to change the color of an active link in the document.
  • An animated button can be created by the Image object.
  • The hash property can be used to access a particular topic in a document.
  • Protocol is the first component of a URL.
  • The location object stores the URL of the current page.
  • The hostname and port of a URL can be retrieved by using the host property of the location object.
  • The href property is used to retrieve the entire URL of a page.
  • The history object list the URLs of the pages visited by a user.
  • The pathname property belongs to the location object. It refers to the directory structure relative to the server’s root directory. The directory names are separated by a single slash (/) within the root.
  • The hash property is used to direct a browser to a particular place in a document. This particular place is called anchor.
  • The navigator object is used to know the name and version of a browser.
  • The name of a browser can be used by using the appName property of the Navigator object.
  • The userAgent property returns the name, version, and platform of the client’s browser.

JavaScript Language Objects

  • String, Math, Array, and Date are language objects.
  • The lastindexof() method is used to start the search of a string from the last characters in the given string.
  • The string object is a global object that is used to modify strings.
  • The indexOf() method is used to search a string from the beginning of a given string and generates the index value for that string.
  • The indexOf() method is used to search a string from the beginning in a given string.
  • The “”
    “” is used to insert a new line within a string.
  • The sentence can behave as a string object if a property or a method of a string object is invoked on that sentence.
  • The bold method is used to format a string to bold.
  • The toUppercase() function changes the whole string to uppercase.
  • The write() method can be used to create a Web page dynamically.
  • The charAt() method is used to retrieve a character in a string by specifying the index as an argument.
  • The \ character is used to display special inline characters, which cannot be displayed directly in a browser.
  • The fixed method of the string object is used to display a text as a typewriter text.
  • The output of the sub method is same as the output of the <sub>…</sub> tag.
  • The sup() function is used to write algebraic expressions.
  • The substring() method is used to cut a small portion of a string from a given long string.
  • The length property is used to retrieve the length of a string.
  • The fontcolor method is used to change the color of text in a string.
  • A number formatted as string behaves as an string. Addition of such numbers is not possible. The numbers are concatenated in this case.
  • The following statements assigns a string to a variable:
    var str=””This is a string.””
    var str=new String(“”This is a string.””)
  • If a search string is not found by using the indexOf() method, the method returns the value -1.
  • The indexOf() and lastIndexOf() methods return -1 if no match is found. The indexOf method starts searching from the beginning, while the lastIndexOf method starts searching from the end.
  • The ASCII code of a character is retrieved by using the charCodeAt() method.
  • The backslash (\) character can be used to display inline characters.
  • The charAt() method is used to extract a single character from a string. The position of the first character in a string is zero.
  • The match() method of the String object is used to search a string on the basis of regular expression. The parameter passed in the parentheses should be a regular expression. If a match is found, it returns the search string, otherwise it returns null.
  • The slice(), substr(), and substring() methods can be used to extract a portion of a string.
  • An array is declared in the following two ways:
    var arr1=new Array(1,2,3,4,5)
    var arr=new Array(5)
  • The arrays in JavaScript can contain any kind of values including null. If an array element contains a null value, it is also counted by the length property
  • The elements of two or more arrays can be joined by using the concat method.
  • The join() method is used to separate the elements in an array by a delimiter.
  • The sort method arranges the elements of an array in ascending order.
  • The push method is used to insert elements in an existing array.
  • The pop() function is used to delete an element from an array.
  • The reverse method of the Array object is used to display the elements of an array in descending order.
  • The push method belongs to the Array object. This method is used to insert elements of the array at the end. A user can insert as many elements in the array as needed.
  • The join method belongs to the Array object. It is used to separate the elements in an array by a delimiter that is passed as an argument in its parentheses.
  • The status property can be used to display a text at the statusbar of a browser window.
  • The current date is displayed by using the Date object.
  • The time is set to milliseconds by using the setTime() method.
  • The cosine of a number is retrieved by using the Math.cos() method.
  • The Math.ceil function is used to return the smallest integer that is greater than or equal to the value provided in the method’s argument.
  • The Math.floor function returns the largest integer that is smaller than or equal to the value provided in the method’s argument.
  • The Math.sqrt function returns the square root of a number provided in the method’s argument.
  • The Math.max value displays the largest value during the comparison operation.
  • The Math.max value displays the smallest value during the comparison operation.

Developing Interactive Forms

  • The elements property of the form object is used to access all the elements in a form.
  • There are three ways of accessing forms in an HTML document.
  • The onclick event handler is the main event handler used with a button object to invoke functions.
  • The encoding property of the form object is used retrieve MINE information of a form.
  • The target property of a form is used to target an output in another frame in a multiframe window.
  • The length property is used to count the number of elements in an array.
  • The target property belongs to the form object. This property is used when the response from a server is to be sent to a particular window or frame. The target property works in the same way as the target attribute of the <form> tag.
  • The button attribute of the <input> tag is used to create a button on a browser.
  • The submit button is used to send a form data to the URL of the server. The URL is defined in the action attribute of the <form> tag.
  • The onclick event handler is most frequently used with a button object.
  • The form data can be sent to a Web server by the submit button.
  • Java provides the facility to create user-defined classes, objects, constructors, and methods. A user can create his package of classes and can reuse them. A user-defined class can also inherit the constructors and methods of different classes that are in-built in the Java language.
  • The checkbox object allows a user to select several items given on a Web page.
  • A checkbox object is one that can be used to select multiple items from the given options.
  • The checked property belongs to the checkbox and radio button object. This property refers to the checked attribute of the <input> tag. It can be used to check or uncheck a checkbox or a radio button.
  • The text element of the <input> tag is used to create a single line text field.
  • The onblur event handler is fired when an object loses the focus.
  • The password field allows the users to type their passwords as asterisks.
  • The select() method belongs to text, textarea, and password objects. This method is used to select all the text present on these objects.
  • A radio button object allows to select only one item from the given options.
  • The event handlers used with the select object are onblur, onfocus, and onchange.
  • The selectIndex property of the select object is used to select an object in the list according to the index of the element.
  • The length property of the select object is used to determine the number of elements in the select object.

Cookies and JavaScript Security

  • Signed script policy is a policy in which testing is required to check the identity of a user sending requests from a Web browser.
  • The domain name of a cookie should match with the domain name of the host so that the cookie can be transferred successfully.
  • The name-value pair is mandatory for creating a cookie object.
  • A cookie is an object used to create and store cookies.
  • The name-value pair is mandatory for creating a cookie. A cookie is used to store small pieces of information on client computers.
  • The path-path pair is used to track the path of a cookie.
  • In Internet Explorer, the cookies are saved in the cookie folder as different cookie files.
  • A cookie is deleted manually by changing its current expiry date to an earlier date.
  • Cookies can be deleted by setting an expiry date for them.
  • A cookie is deleted after the end of a user’s session if the expiry date for that cookie is not set.
  • Time in GMT string is a format used to set the expiry date for a cookie.

Controlling Frames in JavaScript

  • A frame can be accessed by another frame through the frame’s name or the frame’s index.
  • The parent.frames[1] will target the second frame in a multiframe window.
  • A user can define any number of methods for a given object.

Custom JavaScript Objects

  • The constructor is a parameterized function. It is used to create a custom object.
  • A simple custom object contains only one level of properties and methods.

\

Comments are closed.