• +91 9971497814
  • info@interviewmaterial.com

Computer Interview Questions Answers

Java Script Interview Questions Answers

Question - 31 : - How to add Buttons in JavaScript?

Answer - 31 : - The most basic and ancient use of buttons are the "submit" and "clear", which appear slightly before the Pleistocene period. Notice when the "GO!" button is pressed it submits itself to itself and appends the name in the URL. <form action="" name="buttonsGalore" method="get"> Your Name: <input type="text" name="mytext" /> <br /> <input type="submit" value="GO!" /> <input type="reset" value="Clear All" /> </form> Another useful approach is to set the "type" to "button" and use the "onclick" event. <script type="text/javascript"> function displayHero(button) { alert("Your hero is \""+button.value+"\"."); } </script> <form action="" name="buttonsGalore" method="get"> <fieldset style="margin: 1em; text-align: center;"> <legend>Select a Hero</legend> <input type="button" value="Agamemnon" onclick="displayHero(this)" /> <input type="button" value="Achilles" onclick="displayHero(this)" /> <input type="button" value="Hector" onclick="displayHero(this)" /> <div style="height: 1em;" /> </fieldset> </form>

Question - 32 : - How tp create Arrays using JavaScript ?

Answer - 32 : - <script type="text/javascript"> var days = new Array(); days[0] = "Sunday" days[1] = "Monday" days[2] = "Tuesday" days[3] = "Wednesday" days[4] = "Thursday" days[5] = "Friday" days[6] = "Saturday" document.write("first day is "+days[0]) </script> This produces first day is Sunday A more compact way of creating an array is the literal notation: <script type="text/javascript"> var days = ["Sunday","Monday","Tuesday","Wednesday", "Thursday","Friday","Saturday"]; document.write("first day is "+days[0]) </script> This produces first day is Sunday

Question - 33 : - How do  I debug JavaScript?

Answer - 33 : - There are not that many tools out there that will support both client-side and server-side debugging. I am certain this will change as AJAX applications proliferate. I currently do my client-side and server-side debugging separately. Below is some information on the client-side debuggers on some of the commonly used browsers. * Firefox/Mozilla/Netscape - Have a built in debugger Venkman which can be helpful but there is a Firefox add on known as FireBug which provides all the information and AJAX developer would ever need including the ability to inspect the browser DOM, console access to the JavaScript runtime in the browser, and the ability to see the HTTP requests and responses (including those made by an XMLHttpRequest). I tend to develop my applications initially on Firefox using Firebug then venture out to the other browsers. * Safari - Has a debugger which needs to be enabled. See the Safari FAQ for details. * Internet Explorer - There is MSDN Documentation on debugging JavaScript. A developer toolbar for Internet Explorer may also be helpful. While debuggers help a common technique knowing as "Alert Debugging" may be used. In this case you place "alert()" function calls inline much like you would a System.out.println. While a little primitive it works for most basic cases. Some frameworks such as Dojo provide APIs for tracking debug statements.

Question - 34 : - How to get the contents of an input box using Javascript?

Answer - 34 : - Use the "value" property. var myValue = window.document.getElementById("MyTextBox").value;

Question - 35 : - 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?

Answer - 35 : - 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. 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 viewstate MAC failed. What could be one reason that is causing this error? The most common reason for this error is that the machinekey value in machine.config is different for each server. As a result, viewstate 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 machinekey. 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; } } }

Question - 36 : - How to hide javascript code from old browsers that dont run it?

Answer - 36 : - 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

Question - 37 : - How to Add new elements dynamically ?

Answer - 37 : - <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>t1</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>

Question - 38 : - What does isNaN function do?

Answer - 38 : - Return true if the argument is not a number.

Question - 39 : - Are you concerned that older browsers don't support JavaScript and thus exclude a set of Web users? individual users?

Answer - 39 : - Fragmentation of the installed base of browsers will only get worse. By definition, it can never improve unless absolutely everyone on the planet threw away their old browsers and upgraded to the latest gee-whiz versions. But even then, there are plenty of discrepancies between the scriptability of the latest Netscape Navigator and Microsoft Internet Explorer. The situation makes scripting a challenge, especially for newcomers who may not be aware of the limitations of earlier browsers. A lot of effort in my books and ancillary material goes toward helping scripters know what features work in which browsers and how to either workaround limitations in earlier browsers or raise the compatibility common denominator. Designing scripts for a Web site requires making some hard decisions about if, when, and how to implement the advantages scripting offers a page to your audience. For public Web sites, I recommend using scripting in an additive way: let sufficient content stand on its own, but let scriptable browser users receive an enhanced experience, preferably with the same HTML document.

Question - 40 : - What is a prompt box?

Answer - 40 : - A prompt box allows the user to enter input by providing a text box.

Computer Contributors

Rajeev Katiyar
Yes Baroda

Share your email for latest updates


Our partners