Tuesday 3 January 2012

Java Script


What is the result of '1' + 2 + 3?
123
What is the result of 1 + '2' + 3?
123
What is the result of 1 + 2 + '3'?
33
Where are cookies actually stored on the hard disk?
This depends on the user's browser and OS. In the case of Netscape with Windows OS,all the cookies are stored in a single file called cookies.txt c:\Program Files\Netscape\Users\username\cookies.txt In the case of IE,each cookie is stored in a separate file namely username@website.txt. c:\Windows\Cookies\username@Website.txt 33
How to set a HTML document's background color using Javascript?
By using the document.bgcolor property.
What is the use of isNaN function?
NaN stands for Not A Number. This function returns true if the argument is not a number.
What is the data type of variables of in JavaScript?
All variables are of object type in JavaScript.
How to set the focus in an element using Javascript?
What is the difference between RegisterClientScriptBlock and RegisterStartupScript?
RegisterClientScriptBlock emits the JavaScript just after the opening tag. RegisterStartupScript emits the JavaScript at the bottom of the ASP.NET page just before the closing tag
How do you submit a form using JavaScript?
Use document.forms[0].submit() (0 refers to the index of the form – if we have more than one form in a page, then the first one has the index 0, second has index 1 and so on).
How to read and write a file using JavaScript?
I/O operations like reading or writing a file is not possible with client-side JavaScript.
What are JavaScript Data Types?
JavaScript Data Types are Number, String, Boolean, Function, Object, Null, Undefined
How to create arrays in JavaScript?
We can declare an array like this
var scripts = new Array()
We can add elements to this array like this
scripts[0] = "First"
scripts[1] = "Second"
scripts[2] = "third"
scripts[3] = "Fourth"

Now our array scripts have 4 elements inside it and we can print or access them by using their index number. Note that index number starts from 0. To get the third element of the array we have to use the index number 2. Here is the way to get the third element of an array.
document.write(scripts[2])
We also can create an array like this
var no_array = new Array(21, 22, 23, 24, 25)
Difference between JavaScript and Java? 
JavaScript was developed by Brendan Eich of Netscape. Java was developed at Sun Microsystems. While the two languages share some common syntax, they were developed independently of each other and for different audiences. Java is a full-fledged programming language tailored for network computing it includes hundreds of its own objects, including objects for creating user interfaces that appear in Java applets (in Web browsers) or standalone Java applications. In contrast, JavaScript relies on whatever environment it's operating in for the user interface, such as a Web document's form elements. JavaScript was initially called LiveScript at Netscape while it was under development. A licensing deal between Netscape and Sun at the last minute let Netscape plug the "Java" name into the name of its scripting language. Programmers use entirely different tools for Java and JavaScript. It is also not uncommon for a programmer of one language to be ignorant of the other. The two languages don't rely on each other and are intended for different purposes. In some ways, the "Java" name on JavaScript has confused the world's understanding of the differences between the two. On the other hand, JavaScript is much easier to learn than Java and can offer a gentle introduction for newcomers who want to graduate to Java and the kinds of applications you can develop with it
What is the difference between the Methods GET and POST in HTML forms?
GET: Parameters are passed in the query string. Maximum amount of data that can be sent via the GET method is limited to about 2kb. POST: Parameters are passed in the request body. There is no limit to the amount of data that can be transferred using POST. However, there are limits on the maximum amount of data that can be transferred in one name/value pair.
How to write a script for "Select" lists using JavaScript? 
1. To remove an item from a list set it to null mySelectObject.options[3] = null.
2. To truncate a list set its length to the maximum size you desire mySelectObject.length = ?.
3. To delete all options in a select object set the length to 0. mySelectObject.leng=0.
Is a JavaScript script faster than an ASP script? 
Yes. Since JavaScript is a client-side script it does require the web server's help for its computation, so it is always faster than any server-side script like ASP, PHP, etc..
What Boolean operators does JavaScript support? 
Boolean operators in JavaScript are as under &&, || and !
What are the ways to emit client-side JavaScript from server-side code in ASP. NET?
The Page object in ASP. NET has two methods that allow emitting of client-side JavaScript: RegisterClientScriptBlock and RegisterStartupScript. Example usage:
Page.RegisterClientScriptBlock("ScriptKey", "<script language="javascript;">" + "function TestFn() { alert('Clients-side JavaScript'); }</script>"); Page.RegisterStartupScript("ScriptKey", "<script language="javascript">" + "function TestFn() { alert('Clients-side JavaScript'); }</script>");.
ScriptKey is used to suppress the same JavaScript from being emitted more than once. Multiple calls to RegisterClientScriptBlock or RegisterStartupScript with the same value of ScriptKey emit the script only once, on the first call.
What is the difference between RegisterClientScriptBlock and RegisterStartupScript?
RegisterClientScriptBlock emits the JavaScript just after the opening tag. RegisterStartupScript emits the JavaScript at the bottom of the ASP. NET page just before the closing tag.
How to get the contents of an input (text) box using JavaScript?
Use the "value" property. var myValue = window.document.getElementById("MyTextBox").value;
How to determine the state of a checkbox using JavaScript?
Determining the state of a checkbox in JavaScript var checkedP = window.document.getElementById("myCheckBox").checked;
What is the difference between an alert box and a confirmation box? 
An alert box displays only one button which is the OK button whereas the Confirm box displays two buttons namely OK and cancel.
What is a prompt box? 
A prompt box allows the user to enter input by providing a text box.
What looping structures are there in JavaScript? 
JavaScript supports the for loop, while loop, do-while loop, but there is no foreach loop in JavaScript.
To put a "close window" link on a page? 
<a href='javascript:window.close()' class='anyCSSClass'> Close </a>
How to hide JavaScript code from old browsers that don't run it?
Use the below specified style of comments <script language=javascript> <!-- javascript code goes here // --> or Use the <NOSCRIPT>some html code </NOSCRIPT> tags and code the display html statements between these and this will appear on the page if the browser does not support JavaScript
What does JavaScript null mean? 
The null value is a unique value representing no value or no object. It implies no object, or null string, no valid Boolean value, no number and no array object.
How do you assign object properties?
obj["age"] = 17; //or obj.age = 17;
What is this keyword? 
In JavaScript this keywork refers to the current object.
To set all checkboxes to true using JavaScript?
//select all input tags function SelectAll() { var checkboxes = document.getElementsByTagName("input"); for(i=0;i<checkboxes.length;i++) { if(checkboxes.item(i).attributes["type"].value == "checkbox") { checkboxes.item(i).checked = true; } } }
What is the difference between undefined value and null value?
(i) Undefined value cannot be explicitly stated that is there is no keyword called undefined whereas null value has keyword called null (ii) typeof undefined variable or property returns undefined whereas typeof null value returns object
How to find the selected radio button immediately using the 'this' variable? 
<script>
function favAnimal(button) {
alert('You like '+button.value+'s.');
}
</script>
<input type="radio" name="marsupial" value="kangaroo"
onchange="favAnimal(this)">Kangaroo
<br /><input type="radio" name="marsupial" value="Opossum"
onchange="favAnimal(this)">Opossum
<br /><input type="radio" name="marsupial" value="Tasmanian Tiger"
onchange="favAnimal(this)">Hello there
How to find radio button selection when a form is submitted?
<script type="text/javascript">
function findButton() {
var myForm = document.forms.animalForm;
var i;
for(i=0;i<myForm.marsupial.length; i++) {
if(myForm.marsupial[i].checked) {
break;
}
}
alert("You selected ""+myForm.marsupial[i].value+"".");
}
</script>
<form name="animalForm" action="">
<input type="radio" name="marsupial" value="kangaroo" />Kangaroo
<br /><input type="radio" name="marsupial" value="Opossum" />Opossum
<br /><input type="radio" name="marsupial" value="Tasmanian Tiger" />Hello there

<input type="button" name="GO" value="GO" onclick="findButton()" />
How to disable an HTML control or object? 
document.getElementById("myObject").disabled = true;
To write messages to the screen without using "document.write()"? 
function showStatus(message) {
var element = document.getElementById("mystatus");
element.textContent = message; //for Firefox
element.innerHTML = message; //for IE (why can't we all just get along?)
return true;
}
How to Add new element or control dynamically?
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Title of page</title>
<script type="text/javascript">
function addNode() {
var newP = document.createElement("p");
var textNode = document.createTextNode(" I'm a new text node");
newP.appendChild(textNode);
document.getElementById("firstP").appendChild(newP);
}
</script>
</head>
<body onload="addNode();" style=" background: url('../images/Sand-1280.jpg'); background-color: yellow;">
<p id="firstP">firstP<p>
</body>
</html>


No comments:

Post a Comment