You are on page 1of 8

Q31. How to place javascript in HTML page. To insert a JavaScript into an HTML page, use the <script> tag.

Inside the <script> tag use the type attribute to define the scripting language. The <script> and </script> tells where the JavaScript starts and ends: <html> <body> <h1>My First Web Page</h1> <p id="demo">This is a paragraph.</p> <script type="text/JavaScript"> ... some JavaScript code ... </script> </body> </html> The lines between the <script> and </script> contain the JavaScript and are executed by the browser. Q32. What is the difference between VBscript and JavaScript? Differences between VBscript and JavaScript are: 1. JavaScript is the default scripting language for browsers but VBScript must be specified as the scripting language. 2. JavaScript has cross-platform support from all popular browsers while VBScript is supported MS IE only. VBScripters would thus loose a sizable audience. 3. One of the most significant issues with JavaScript is that there were different releases of the language since its inception (version 1.0). Similarly, different versions of browsers exist on users machines. Therefore, code written for one version may not necessarily work on another. More testing would be necessary thus, development time increases. JavaScript is case sensitive but VBScript is not this would not be prone to as many syntax errors.< JavaScript uses the same character for concatenation as it does for addition (the + character) while the '&' concatenating character is used in VBScript. This is another source of errors in JavaScript.

4. 5.

Q32(a). What are the similarities of javascript and vbscript? Similarities of javascript and vbscript are: 1. Both languages are easy to learn and do not require any expensive development tools 2. Both can be used to enhance web pages 3. 4. They run on client machines and can substitute CGI programs to reduce server loads Both can abuse and run malicious scripts on clients' machines

Q33. What are the rules for naming JavaScript variables? Rules for JavaScript variable names: Variable names are case sensitive (y and Y are two different variables) Variable names must begin with a letter, the $ character, or the underscore character Note: Because JavaScript is case-sensitive, variable names are case-sensitive. Q34. Describe JavaScript operators with example. = is used to assign values. + is used to add values. The assignment operator = is used to assign values to JavaScript variables. The arithmetic operator + is used to add values together. y=5; z=2; x=y+z; The value of x, after the execution of the statements above, is 7. JavaScript Arithmetic Operators Arithmetic operators are used to perform arithmetic between variables and/or values. Given that y=5, the table below explains the arithmetic operators:

Operato Description r + Addition Subtraction * Multiplication / Division % Modulus remainder) ++ Increment

Example x=y+2 x=y-2 x=y*2 x=y/2 (division x=y%2

Result x=7 x=3 x=10 x=2.5 x=1 y=5 y=5 y=5 y=5 y=5

x=++y x=6 y=6 x=y++ x=5 y=6 -Decrement x=--y x=4 y=4 x=y-x=5 y=4 JavaScript Assignment Operators Assignment operators are used to assign values to JavaScript variables. Given that x=10 and y=5, the table below explains the assignment operators: Operat Example Same As Result or = x=y x=5 += x+=y x=x+y x=15 -= x-=y x=x-y x=5 *= x*=y x=x*y x=50 /= x/=y x=x/y x=2 %= x%=y x=x%y x=0 The + Operator Used on Strings The + operator can also be used to add string variables or text values together. To add two or more string variables together, use the + operator. txt1="What a very"; txt2="nice day"; txt3=txt1+txt2; After the execution of the statements above, the variable txt3 contains "What a verynice day". To add a space between the two strings, insert a space into one of the strings: txt1="What a very "; txt2="nice day"; txt3=txt1+txt2; or insert a space into the expression: txt1="What a very"; txt2="nice day"; txt3=txt1+" "+txt2; After the execution of the statements above, the variable txt3 contains: "What a very nice day" Adding Strings and Numbers The rule is: If you add a number and a string, the result will be a string! Example x=5+5; document.write(x); x="5"+"5"; document.write(x); x=5+"5"; document.write(x); x="5"+5; document.write(x); Q34(a). Explain with code how to convert strings to numbers in javascript? One very common conversion that you will often need to do in your Javascript is to convert text string into numbers. The reason for this is that HTML does not provide any means for the people visiting your site to enter numbers into any input fields on your web page that will actually be considered to be a number field in the first place. HTML only provides our visitors with a means of entering text strings and it is then up to us to convert that text string into a number if that is what the text is supposed to contain.

Javascript provides a number of different ways that we can use to convert text into a number. There are a few things we need to take into account when convertiing a text string into a number using any of the available methods and these may affect which of the various methods is the right one for you to choose. Does the text string actually contain a valid number? if it doesn't then we will not be able to convert it to one. Do we want the number to be an integer or can it have decimal places? If it can have decimal places how many is it allowed to have?

First let's look at a list of the various ways that we can convert a test string into a number. var numValue = +textValue; var numValue = textValue - 0; var numValue = textValue * 1; var numValue = textValue / 1; var numValue = Number(textValue); var numValue = parseFloat(textValue,10); var numValue = parseInt(textValue,10); Using a unary plus operator on the text string is the quickest and easiest way of converting a text string into a number and works regardless of whether the number is positive or negative. If a nonnumerical value is contained in the text string then NaN will be the assigned result indicating that it is not a number. Subtracting zero or multiplying or dividing by one have the exact same effect as using the unary plus except for involving slightly longer code for the conversion. Calling the Number() function to perform the conversion of the text field to a number is slower than any of the prior solutions simply due to the fact that it needs to call a function in order to perform the conversion rather than performing the conversion directly. The big advantage that it does have over the prior methods is that it makes it completely obvious what the code is actually doing and so if the efficiency of the code is not a major concern then this may be the best option to choose. The final two methods shown that use parseFloat() and parseInt() are slightly different from all of the preceding methods in that the primary purpose of these functions is not the conversion of a text string into a number. These functions are actually intended to be used to convert numbers from one number base to another (for example from hexadecimal - base 16 - to decimal - base 10). In order to be able to convert the number to the desired base the value to be converted needs to be a number and so if a text string is supplied instead then that text string gets converted to a number in order for the function to use it as an input. The method that these two functions use to do the number conversion is different from that of the earlier methods shown in that instead of treating the whole text string as a whole and returning NaN if it is not a number, these functions instead extract as much from the start of the text string as can be converted into a number and will discard any non-numeric value that follows the number. This makes these functions useful where you have a text string that you know starts with a number and which may be followed by something else. The parseInt() function assumes that the number you want must be an integer and so a period or exponential is discarded while parseFloat() will include decimal places and exponentials in the number extracted. These two functions will always extract an actual number since if the text string doesn't start with a number they will return zero. With the 10 (indicating base 10) omitted the functions examine the start of the number to determine the number base and any number starting with '0x' will be considered to be hexadecimal (and hence 'A' through 'F' will be considered to be numbers) while any number starting with '0' may be considered to be octal (and so 8 and 9 will be considered to not be numbers). If you were to specify 36 as the number base for either of these functions then all of the letters of the alphabet would be treated as numbers and a one followed by a zero would represent the number 36. Q34(b). Explain cocatenation of strings in javascript with a program. The most common operations performed with strings is concatenation. Concatenation is a process of combining two strings into one longer string. For example, we could combine the strings "Scripting" and "Master" into a new string as "Scripting Master." "+" symbol is used for string concatenation in JavaScript. This symbol is also used as a mathematical addition operator in JavaScript. So if you are using the "+" with numerical values it will add the two values; if you use the same operator with two strings, it will concatenate (combine into one) two strings. The following shows a simple example: <script language="javascript"> var string1, string2, stringConcatenated; string1 = "Concatenating "; // first string

string2 = "strings"; // second string stringConcatenated = string1 + string2; // Concatenating strings document.write (stringConcatenated); // printing the Concatenated string </script> Q35. What do you understand by javascript Pop-up boxes? JavaScript has three kind of popup boxes: Alert box, Confirm box, and Prompt box. Alert Box An alert box is often used if you want to make sure information comes through to the user. When an alert box pops up, the user will have to click "OK" to proceed. Syntax alert("sometext"); Example <html> <head> <script type="text/javascript"> function show_alert() { alert("I am an alert box!"); } </script> </head> <body> <input type="button" onclick="show_alert()" value="Show alert box" /> </body> </html> Confirm Box A confirm box is often used if you want the user to verify or accept something. When a confirm box pops up, the user will have to click either "OK" or "Cancel" to proceed. If the user clicks "OK", the box returns true. If the user clicks "Cancel", the box returns false. Syntax confirm("sometext"); Example <html> <head> <script type="text/javascript"> function show_confirm() { var r=confirm("Press a button"); if (r==true) { alert("You pressed OK!"); } else { alert("You pressed Cancel!"); } } </script> </head> <body> <input type="button" onclick="show_confirm()" value="Show confirm box" /> </body> </html> Prompt Box A prompt box is often used if you want the user to input a value before entering a page. When a prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed after entering an input value.

If the user clicks "OK" the box returns the input value. If the user clicks "Cancel" the box returns null. Syntax prompt("sometext","defaultvalue"); Example <html> <head> <script type="text/javascript"> function show_prompt() { var name=prompt("Please enter your name","Harry Potter"); if (name!=null && name!="") { document.write("<p>Hello " + name + "! How are you today?</p>"); } } </script> </head> <body> <input type="button" onclick="show_prompt()" value="Show prompt box" /> </body> </html> Q36. What is javascript statement? A JavaScript statement is a command to a browser. The purpose of the command is to tell the browser what to do. This following JavaScript statement tells the browser to write "Hello Dolly" to the web page: document.write("Hello Dolly"); It is normal to add a semicolon at the end of each executable statement. The semicolon is optional and the browser is supposed to interpret the end of the line as the end of the statement. JavaScript Code JavaScript code (or just JavaScript) is a sequence of JavaScript statements. Each statement is executed by the browser in the sequence they are written. This example will write a heading and two paragraphs to a web page: Example <script type="text/javascript"> document.write("<h1>This is a heading</h1>"); document.write("<p>This is a paragraph.</p>"); document.write("<p>This is another paragraph.</p>"); </script> Q37. Are Java and JavaScript the Same? NO! Java and JavaScript are two completely different languages in both concept and design. Java (developed by Sun Microsystems) is a powerful and much more complex programming language in the same category as C and C++. Q38. How to Put a JavaScript Into an HTML Page. To insert a JavaScript into an HTML page, use the <script> tag. Inside the <script> tag use the type attribute to define the scripting language. The <script> and </script> tells where the JavaScript starts and ends. The lines between the <script> and </script> contain the JavaScript and are executed by the browser: <html> <body><script type="text/javascript"> document.write("Hello World!"); </script> </body> </html> Q39. What are the conditional statements in javascript? Conditional statements are used to perform different actions based on different conditions.

Conditional Statements In JavaScript we have the following conditional statements: if statement - use this statement to execute some code only if a specified condition is true if...else statement - use this statement to execute some code if the condition is true and another code if the condition is false if...else if....else statement - use this statement to select one of many blocks of code to be executed switch statement - use this statement to select one of many blocks of code to be executed If Statement Use the if statement to execute some code only if a specified condition is true. Syntax if (condition) { code to be executed if condition is true } Note that if is written in lowercase letters. Using uppercase letters (IF) will generate a JavaScript error! Example <script type="text/javascript"> //Write a "Good morning" greeting if //the time is less than 10 var d=new Date(); var time=d.getHours(); if (time<10) { document.write("<b>Good morning</b>"); } </script> If...else Statement Use the if....else statement to execute some code if a condition is true and another code if the condition is not true. Syntax if (condition) { code to be executed if condition is true } else { code to be executed if condition is not true } Example <script type="text/javascript"> //If the time is less than 10, you will get a "Good morning" greeting. //Otherwise you will get a "Good day" greeting. var d = new Date(); var time = d.getHours(); if (time < 10) { document.write("Good morning!"); } else { document.write("Good day!"); } </script> If...else if...else Statement Use the if....else if...else statement to select one of several blocks of code to be executed. Syntax if (condition1) {

code to be executed if condition1 is true } else if (condition2) { code to be executed if condition2 is true } else { code to be executed if neither condition1 nor condition2 is true } Example <script type="text/javascript"> var d = new Date() var time = d.getHours() if (time<10) { document.write("<b>Good morning</b>"); } else if (time>=10 && time<16) { document.write("<b>Good day</b>"); } else { document.write("<b>Hello World!</b>"); } </script> Q40. Explain different loop statements in javascript. Loops execute a block of code a specified number of times, or while a specified condition is true. In JavaScript, there are two different kind of loops: for - loops through a block of code a specified number of times while - loops through a block of code while a specified condition is true The for Loop The for loop is used when you know in advance how many times the script should run. Syntax for (variable=startvalue;variable<=endvalue;variable=variable+increment) { code to be executed } Example <html> <body> <script type="text/javascript"> var i=0; for (i=0;i<=5;i++) { document.write("The number is " + i); document.write("<br />"); } </script> </body> </html> The while Loop The while loop loops through a block of code while a specified condition is true. Syntax while (variable<=endvalue) { code to be executed } Note: The <= could be any comparing operator. Example

<html> <body> <script type="text/javascript"> var i=0; while (i<=5) { document.write("The number is " + i); document.write("<br />"); i++; } </script> </body> </html>

You might also like