JavaScript

  Home  World Wide Web  JavaScript


“JavaScript Tutorial with interview questions and answers. And thousands of JavaScript real Examples.”



114 JavaScript Questions And Answers

61⟩ What does the term sticky session mean in a web-farm scenario? Why would you use a sticky session? What is the potential disadvantage of using a sticky session?

Sticky session refers to the feature of many commercial load balancing solutions for web-farms to route the requests for a particular session to the same physical machine that serviced the first request for that session. This is mainly used to ensure that a in-proc session is not lost as a result of requests for a session being routed to different servers. Since requests for a user are always routed to the same machine that first served the request for that session, sticky sessions can cause uneven load distribution across servers.

 209 views

62⟩ You have an ASP. NET web application running on a web-farm that does not use sticky sessions - so the requests for a session are not guaranteed to be served the same machine. Occasionally, the users get error message Validation of view state MAC failed. What could be one reason that is causing this error?

The most common reason for this error is that the the machine key value in machine.config is different for each server. As a result, view state encoded by one machine cannot be decoded by another. To rectify this, edit the machine.config file on each server in the web-farm to have the same value for machine key.

 214 views

63⟩ How to select an element by id and swapping an image?

<script language="JavaScript" type="text/javascript" >

function setBeerIcon() {

var beerIcon = document.getElementById("beerIcon");

beerIcon.src = "images/"+getSelectValue("beer")+".jpg";

}

</script>

<img border="0" src="" id="brandIcon" alt="brand" />

<select name="beer" id="beer" onChange="setButton();setBeerIcon();">

<option value="--Select--">Select beer</option>

<option value="heineken">heineken</option>

<option value="sol">sol</option>

<option value="amstellight">amstellight</option>

<option value="coronalight">coronalight</option>

<option value="coronaextra">coronaextra</option>

<option value=""></option>

</select>

 226 views

66⟩ What is variable typing in JavaScript?

It is perfectly legal to assign a number to a variable and then assign a string to the same variable as follows

example

i = 10;

i = "string";

This is called variable typing

 182 views

68⟩ 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;

}

}

}

 195 views

69⟩ What are undefined and undeclared variables?

Undeclared variables are those that are not declared in the program (do not exist at all), trying to read their values gives runtime error. But if undeclared variables are assigned then implicit declaration is done .

Undefined variables are those that are not assigned any value but are declared in the program. Trying to read such variables gives special value called undefined value.

 209 views

70⟩ What is === operator?

In JavaScript === is strict equality operator ,it returns true only when the two operands are having the same value without any type conversion.

 191 views

71⟩ 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)">Tasmanian Tiger

 188 views

72⟩ 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" />Tasmanian Tiger

<input type="button" name="GO" value="GO" onclick="findButton()" />

 218 views

74⟩ To write messages to the screen without using "document.write()"?

Changing the contents of an element is a much better solution. When the method showStatus is invoked it will change the content of the span.

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;

}

<span id="mystatus">Test. </span>

 184 views

75⟩ How to Add new elements 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>

 194 views

78⟩ Explain unescape() and escape() in JavaScript?

These are similar to the decodeURI() and encodeURI(), but escape() is used for only portions of a URI.

<script type="text/javascript">

var myvalue = "Sir Robbert Scott";

document.write("Original myvalue: "+myvalue);

document.write("<br />escaped: "+escape(myvalue));

document.write("<br />uri part: "&author="+escape(myvalue)+""");

</script>

If you use escape() for the whole URI... well bad things happen.

<script type="text/javascript">

var uri = "http://www.google.com/search?q=Online Web Tutorials"

document.write("Original uri: "+uri);

document.write("

escaped: "+escape(uri));

</script>

 274 views