tag:blogger.com,1999:blog-9996607392976701012024-03-12T20:12:13.414-07:00Javascript Interview QuestionsA complete tutorial of javascript Interview Questions,free ebooks along with event-driven, interpreted client-side scripting and programming language,VB script.jainmaneesh12@gmail.comhttp://www.blogger.com/profile/11433144482700682713noreply@blogger.comBlogger97125tag:blogger.com,1999:blog-999660739297670101.post-74250661275444296362008-10-31T22:48:00.000-07:002008-10-31T22:49:01.114-07:00What are undefined and undeclared variables?<span style="font-style:italic;">Undeclared variables</span> 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 .<br /><br /><span style="font-style:italic;">Undefined variables</span> 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.jainmaneesh12@gmail.comhttp://www.blogger.com/profile/11433144482700682713noreply@blogger.comtag:blogger.com,1999:blog-999660739297670101.post-32279364992237468572008-10-31T22:40:00.000-07:002008-10-31T22:41:23.793-07:00How to change style on an element?Between CSS and javascript is a weird symmetry. CSS style rules are layed on top of the DOM. The CSS property names like "font-weight" are transliterated into "myElement.style.fontWeight". The class of an element can be swapped out. For example:<br />document.getElementById("myText").style.color = "green";<br />document.getElementById("myText").style.fontSize = "20";<br />-or-<br />document.getElementById("myText").className = "regular";jainmaneesh12@gmail.comhttp://www.blogger.com/profile/11433144482700682713noreply@blogger.comtag:blogger.com,1999:blog-999660739297670101.post-1601505092825463102008-10-31T22:36:00.000-07:002008-10-31T22:37:05.246-07:00How to set the cursor to wait ?In theory, we should cache the current state of the cursor and then put it back to its original state.<br />document.body.style.cursor = 'wait';<br />//do something interesting and time consuming<br />document.body.style.cursor = 'auto';jainmaneesh12@gmail.comhttp://www.blogger.com/profile/11433144482700682713noreply@blogger.comtag:blogger.com,1999:blog-999660739297670101.post-17317592140716731622008-08-22T21:58:00.000-07:002008-08-22T21:59:03.649-07:00How can JavaScript be used to improve the look and feel of a Web site? By the same token, how can JavaScript be used to improve the user interface?On their own, Web pages tend to be lifeless and flat unless you add animated images or more bandwidth-intensive content such as Java applets or other content requiring plug-ins to operate (Shockwave and Flash, for example).<br /><br />Embedding JavaScript into an HTML page can bring the page to life in any number of ways. Perhaps the most visible features built into pages recently with the help of JavaScript are the so-called image rollovers: roll the cursor atop a graphic image and its appearance changes to a highlighted version as a feedback mechanism to let you know precisely what you're about to click on. But there are less visible yet more powerful enhancements to pages that JavaScript offers.<br /><br />Interactive forms validation is an extremely useful application of JavaScript. While a user is entering data into form fields, scripts can examine the validity of the data--did the user type any letters into a phone number field?, for instance. Without scripting, the user has to submit the form and let a server program (CGI) check the field entry and then report back to the user. This is usually done in a batch mode (the entire form at once), and the extra transactions take a lot of time and server processing power. Interactive validation scripts can check each form field immediately after the user has entered the data, while the information is fresh in the mind. <br /><br />Are you concerned that older browsers don't support JavaScript and thus exclude a set of Web users? individual users? <br />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 script ability of the latest Netscape Navigator and Microsoft Internet Explorer.<br /><br />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 scripter know what features work in which browsers and how to either workaround limitations in earlier browsers or raise the compatibility common denominator.<br /><br />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 documentjainmaneesh12@gmail.comhttp://www.blogger.com/profile/11433144482700682713noreply@blogger.comtag:blogger.com,1999:blog-999660739297670101.post-3060763001927718942008-08-22T21:57:00.002-07:002008-08-22T21:58:27.646-07:00How can JavaScript make a Web site easier to use? That is, are there certain JavaScript techniques that make it easier for people to use a Web site?JavaScript's greatest potential gift to a Web site is that scripts can make the page more immediately interactive, that is, interactive without having to submit every little thing to the server for a server program to re-render the page and send it back to the client. For example, consider a top-level navigation panel that has, say, six primary image map links into subsections of the Web site. With only a little bit of scripting, each map area can be instructed to pop up a more detailed list of links to the contents within a subsection whenever the user rolls the cursor atop a map area. With the help of that popup list of links, the user with a scriptable browser can bypass one intermediate menu page. The user without a scriptable browser (or who has disabled JavaScript) will have to drill down through a more traditional and time-consuming path to the desired content.jainmaneesh12@gmail.comhttp://www.blogger.com/profile/11433144482700682713noreply@blogger.comtag:blogger.com,1999:blog-999660739297670101.post-11001389303805627612008-08-22T21:57:00.001-07:002008-08-22T21:57:31.901-07:00How is JavaScript different from 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.<br /><br />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.jainmaneesh12@gmail.comhttp://www.blogger.com/profile/11433144482700682713noreply@blogger.comtag:blogger.com,1999:blog-999660739297670101.post-57208418567065256562008-08-22T21:56:00.000-07:002008-08-22T21:57:10.873-07:00How can JavaScript be used to personalize or tailor a Web site to fit individual users?JavaScript allows a Web page to perform "if-then" kinds of decisions based on browser version, operating system, user input, and, in more recent browsers, details about the screen size in which the browser is running. While a server CGI program can make some of those same kinds of decisions, not everyone has access to or the expertise to create CGI programs. For example, an experienced CGI programmer can examine information about the browser whenever a request for a page is made thus a server so equipped might serve up one page for Navigator users and a different page for Internet Explorer users. Beyond browser and operating system version, a CGI program can't know more about the environment. But a JavaScript-enhanced page can instruct the browser to render only certain content based on the browser, operating system, and even the screen size.<br /><br />Scripting can even go further if the page author desires. For example, the author may include a preference screen that lets the user determine the desired background and text color combination. A script can save this information on the client in a well-regulated local file called a cookie. The next time the user comes to the site, scripts in its pages look to the cookie info and render the page in the color combination selected previously. The server is none the wiser, nor does it have to store any visitor-specific information.jainmaneesh12@gmail.comhttp://www.blogger.com/profile/11433144482700682713noreply@blogger.comtag:blogger.com,1999:blog-999660739297670101.post-3808092824372449442008-08-22T21:53:00.000-07:002008-08-22T21:54:13.234-07:00What are a fixed-width table and its advantages in JavaScript?Fixed width tables are rendered by the browser based on the widths of the columns in the first row, in JavaScript resulting in a faster display in case of large tables. Use the CSS style table-layout:fixed to specify a fixed width table. <br />If the table is not specified to be of fixed width in JavaScript, the browser has to wait till all data is downloaded and then infer the best width for each of the columns. This process can be very slow for large tables.jainmaneesh12@gmail.comhttp://www.blogger.com/profile/11433144482700682713noreply@blogger.comtag:blogger.com,1999:blog-999660739297670101.post-8110467181828981842008-08-06T22:48:00.000-07:002008-08-06T22:51:37.867-07:00How to have the status line update when the mouse goes over a link ?onmouseover="window.status='Hi There!';return true"<br />onmouseout="window.status='';return true"jainmaneesh12@gmail.comhttp://www.blogger.com/profile/11433144482700682713noreply@blogger.comtag:blogger.com,1999:blog-999660739297670101.post-1121431828323578182008-06-24T22:21:00.000-07:002008-07-22T21:30:26.778-07:00need to set the cursor at the end of text in textboxfunction setSelectionRange(input, selectionStart, selectionEnd) {<br />if (input.setSelectionRange) {<br />input.focus();<br />input.setSelectionRange(selectionStart, selectionEnd);<br />}<br />else if (input.createTextRange) {<br />var range = input.createTextRange();<br />range.collapse(true);<br />range.moveEnd('character', selectionEnd);<br />range.moveStart('character', selectionStart);<br />range.select();<br />}<br />}<br />function setCaretToEnd (input) {<br />setSelectionRange(input, input.value.length, input.value.length);<br />}jainmaneesh12@gmail.comhttp://www.blogger.com/profile/11433144482700682713noreply@blogger.comtag:blogger.com,1999:blog-999660739297670101.post-56728811679203461132008-06-24T22:20:00.000-07:002008-06-24T22:21:02.389-07:00Detect special characters in text box. Or any character you subsitute for the special characters.var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";<br /> for (var i = 0; i < document.formname.fieldname.value.length; i++) {<br /> if (iChars.indexOf(document.formname.fieldname.value.charAt(i)) != -1) {<br /> alert ("The box has special characters. \nThese are not allowed.\n");<br /> return false;<br /> }<br /> }jainmaneesh12@gmail.comhttp://www.blogger.com/profile/11433144482700682713noreply@blogger.comtag:blogger.com,1999:blog-999660739297670101.post-17829007761755830492008-06-20T22:17:00.001-07:002008-06-20T22:17:53.811-07:00Remove special characters from a string.Remove special characters from a string.<br /><br />function clearText() {<br /> document.formname.fieldname.value=filterNum(document.formname.fieldname.value)<br /><br /> function filterNum(str) {<br /> re = /\$|,|@|#|~|`|\%|\*|\^|\&|\(|\)|\+|\=|\[|\-|\_|\]|\[|\}|\{|\;|\:|\'|\"|\<|\>|\?|\||\\|\!|\$|\./g;<br /> // remove special characters like "$" and "," etc...<br /> return str.replace(re, "");<br /> }<br />}jainmaneesh12@gmail.comhttp://www.blogger.com/profile/11433144482700682713noreply@blogger.comtag:blogger.com,1999:blog-999660739297670101.post-73158160775528565682008-06-20T22:16:00.000-07:002008-06-20T22:17:08.046-07:00Scan values in a field and if they are all letters then alert. The second block of code does the same but for numbers//alert on finding all letters<br /> var noalpha = /^[a-zA-Z]*$/;<br />if (noalpha.test(document.formname.fieldname.value)) {<br /> alert("Please enter at least one number in the \"username\" field.");<br /> return false;<br />}<br /><br />//alert on finding all numbers<br />var nonums = /^[0-9]*$/;<br />if (nonums.test(document.formname.fieldname.value)) {<br /> alert("Please enter at least one letter in the \"username\" field.");<br /> return false;<br />}jainmaneesh12@gmail.comhttp://www.blogger.com/profile/11433144482700682713noreply@blogger.comtag:blogger.com,1999:blog-999660739297670101.post-21016014665892289182008-06-20T22:15:00.000-07:002008-06-20T22:16:28.636-07:00Check drop down has been selected. Set drop down's value to Not_Selected for this to work.if (document.formname.fieldname.value == 'Not_Selected') <br />{<br /> alert("Please provide us with a selection.\n");<br /> return false;<br />}jainmaneesh12@gmail.comhttp://www.blogger.com/profile/11433144482700682713noreply@blogger.comtag:blogger.com,1999:blog-999660739297670101.post-28807318737049694792008-06-19T22:22:00.000-07:002008-06-19T22:23:04.325-07:00Check if multiple checkboxes have not been selected.Replace false with true to see if all are selected.if (document.formname.fieldname.checked == false &&<br /> document.formname.fieldname.checked == false &&<br /> document.formname.fieldname.checked == false) {<br /> <br /> alert("Please select at least one checkbox.\n");<br /> return false;<br /> }jainmaneesh12@gmail.comhttp://www.blogger.com/profile/11433144482700682713noreply@blogger.comtag:blogger.com,1999:blog-999660739297670101.post-37107541154386774902008-06-19T22:21:00.000-07:002008-06-19T22:22:31.716-07:00Check if textbox has any characters in it.if (document.formname.fieldname.value.length == 0) {<br /> alert("Please fill out your name.\n");<br /> return false;<br /> }jainmaneesh12@gmail.comhttp://www.blogger.com/profile/11433144482700682713noreply@blogger.comtag:blogger.com,1999:blog-999660739297670101.post-55169566431721509022008-06-16T21:41:00.000-07:002008-06-16T21:42:14.047-07:00Check if certian radio buttons have not been selectedif (!document.formname.fieldname[0].checked &&<br /> !document.formname.fieldname[1].checked &&<br /> !document.formname.fieldname[2].checked &&<br /> !document.formname.fieldname[3].checked) {<br /> alert("Please choose a group designation.\n");<br /> return false;<br /> }jainmaneesh12@gmail.comhttp://www.blogger.com/profile/11433144482700682713noreply@blogger.comtag:blogger.com,1999:blog-999660739297670101.post-2219502970682869392008-06-16T21:40:00.002-07:002008-06-16T21:41:14.840-07:00Check if field has special charactersvar iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";<br /><br /> for (var i = 0; i < document.formname.fieldname.value.length; i++) {<br /> if (iChars.indexOf(document.formname.fieldname.value.charAt(i)) != -1) {<br /> alert ("Your username has special characters. \nThese are not allowed.\n Please remove them and try again.");<br /> return false;<br /> }<br /> }jainmaneesh12@gmail.comhttp://www.blogger.com/profile/11433144482700682713noreply@blogger.comtag:blogger.com,1999:blog-999660739297670101.post-63370158159676979272008-06-16T21:40:00.001-07:002008-06-16T21:40:47.851-07:00E-Mail format checking scriptif (document.formname.fieldname.value.length >0) {<br /> i=document.formname.fieldname.value.indexOf("@")<br /> j=document.formname.fieldname.value.indexOf(".",i)<br /> k=document.formname.fieldname.value.indexOf(",")<br /> kk=document.formname.fieldname.value.indexOf(" ")<br /> jj=document.formname.fieldname.value.lastIndexOf(".")+1<br /> len=document.formname.fieldname.value.length<br /><br /> if ((i>0) && (j>(1+1)) && (k==-1) && (kk==-1) && (len-jj >=2) && (len-jj<=3)) {<br /> }<br /> else {<br /> alert("Please enter an exact email address.\n" +<br /> document.formname.fieldname.value + " is invalid.");<br /> return false;<br /> }<br /><br /> }jainmaneesh12@gmail.comhttp://www.blogger.com/profile/11433144482700682713noreply@blogger.comtag:blogger.com,1999:blog-999660739297670101.post-9291374146156708972008-06-15T21:53:00.000-07:002008-06-15T21:54:26.500-07:00Validation checks if the phone number is validThe function below checks if the phone number is valid. At first we use regular expression and the replace() method to clear out any spacer characters. Next, we use the isNaN() function to check if the phone number contain only numbers. At last we check the length of the string and permit only phone numbers with 10 digits.<br /><br />function validatePhone(fld) {<br /> var error = "";<br /> var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, ''); <br /><br /> if (fld.value == "") {<br /> error = "You didn't enter a phone number.\n";<br /> fld.style.background = 'Yellow';<br /> } else if (isNaN(parseInt(stripped))) {<br /> error = "The phone number contains illegal characters.\n";<br /> fld.style.background = 'Yellow';<br /> } else if (!(stripped.length == 10)) {<br /> error = "The phone number is the wrong length. Make sure you included an area code.\n";<br /> fld.style.background = 'Yellow';<br /> } <br /> return error;<br />}jainmaneesh12@gmail.comhttp://www.blogger.com/profile/11433144482700682713noreply@blogger.comtag:blogger.com,1999:blog-999660739297670101.post-43981181168213195432008-06-15T21:51:00.002-07:002008-06-15T21:52:42.061-07:00Validation For EmailNext we want to see if the email address the user entered is real. This means that the input data must contain at least an @ sign and a dot (.). Also, the @ must not be the first character of the email address, and the last dot must at least be one character after the @ sign. <br />At first we check if the user entered anything at all in the email field. Next, we use regular expression and the test() method to check the field for a compliance. Also we will use trim() function that will trim leading whitespace off the string. This won’t be perfect validation — it is possible to slip not compliant addresses by it — but it's normally good enough.<br /><br /><br />function trim(s)<br />{<br /> return s.replace(/^\s+|\s+$/, '');<br />} <br /><br />function validateEmail(fld) {<br /> var error="";<br /> var tfld = trim(fld.value); // value of field with whitespace trimmed off<br /> var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;<br /> var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;<br /> <br /> if (fld.value == "") {<br /> fld.style.background = 'Yellow';<br /> error = "You didn't enter an email address.\n";<br /> } else if (!emailFilter.test(tfld)) { //test email for illegal characters<br /> fld.style.background = 'Yellow';<br /> error = "Please enter a valid email address.\n";<br /> } else if (fld.value.match(illegalChars)) {<br /> fld.style.background = 'Yellow';<br /> error = "The email address contains illegal characters.\n";<br /> } else {<br /> fld.style.background = 'White';<br /> }<br /> return error;<br />}jainmaneesh12@gmail.comhttp://www.blogger.com/profile/11433144482700682713noreply@blogger.comtag:blogger.com,1999:blog-999660739297670101.post-65883010690351284592008-06-15T21:51:00.001-07:002008-06-15T21:51:32.343-07:00Validation below checks the password field for blankness and allow only letters and numbersThe function below checks the password field for blankness and allow only letters and numbers - no underscopes this time. So we should use a new regular expression to forbid underscopes. This one /[\W_]/ allow only letters and numbers. Next, we want to permit only passwords that contain letters and at least one numeral. For that we use the seacrh() method and two more regular expressions: /(a-z)+/ and /(0-9)/.<br /><br />function validatePassword(fld) {<br /> var error = "";<br /> var illegalChars = /[\W_]/; // allow only letters and numbers <br /> <br /> if (fld.value == "") {<br /> fld.style.background = 'Yellow';<br /> error = "You didn't enter a password.\n";<br /> } else if ((fld.value.length < 7) || (fld.value.length > 15)) {<br /> error = "The password is the wrong length. \n";<br /> fld.style.background = 'Yellow';<br /> } else if (illegalChars.test(fld.value)) {<br /> error = "The password contains illegal characters.\n";<br /> fld.style.background = 'Yellow';<br /> } else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {<br /> error = "The password must contain at least one numeral.\n";<br /> fld.style.background = 'Yellow';<br /> } else {<br /> fld.style.background = 'White';<br /> }<br /> return error;<br />}jainmaneesh12@gmail.comhttp://www.blogger.com/profile/11433144482700682713noreply@blogger.comtag:blogger.com,1999:blog-999660739297670101.post-43926771978854458302008-06-15T21:50:00.001-07:002008-06-15T21:50:35.458-07:00Validation checks if the user entered anything at all in the username fieldThe function below checks if the user entered anything at all in the username field. If it’s not blank, we check the length of the string and permit only usernames that are between 5 and 15 characters. Next, we use the JavaScript regular expression /\W/ to forbid illegal characters from appearing in usernames. We want to allow only letters, numbers and underscopes.<br /><br />function validateUsername(fld) {<br /> var error = "";<br /> var illegalChars = /\W/; // allow letters, numbers, and underscores<br /> <br /> if (fld.value == "") {<br /> fld.style.background = 'Yellow'; <br /> error = "You didn't enter a username.\n";<br /> } else if ((fld.value.length < 5) || (fld.value.length > 15)) {<br /> fld.style.background = 'Yellow'; <br /> error = "The username is the wrong length.\n";<br /> } else if (illegalChars.test(fld.value)) {<br /> fld.style.background = 'Yellow'; <br /> error = "The username contains illegal characters.\n";<br /> } else {<br /> fld.style.background = 'White';<br /> } <br /> return error;<br />}jainmaneesh12@gmail.comhttp://www.blogger.com/profile/11433144482700682713noreply@blogger.comtag:blogger.com,1999:blog-999660739297670101.post-36674390838974385192008-06-15T21:49:00.001-07:002008-06-15T21:49:41.186-07:00Validation checks if a required field has been left emptyThe function below checks if a required field has been left empty. If the required field is blank, we return the error string to the main function. If it’s not blank, the function returns an empty string.<br /><br />function validateEmpty(fld) {<br /> var error = "";<br /> <br /> if (fld.value.length == 0) {<br /> fld.style.background = 'Yellow'; <br /> error = "The required field has not been filled in.\n"<br /> } else {<br /> fld.style.background = 'White';<br /> }<br /> return error; <br />}jainmaneesh12@gmail.comhttp://www.blogger.com/profile/11433144482700682713noreply@blogger.comtag:blogger.com,1999:blog-999660739297670101.post-2116120995029219462008-06-15T21:46:00.000-07:002008-06-15T21:48:14.215-07:00Validation on Form SubmitionThis is a main function that calls a series of subfunctions, each of which checks a single form element for compliance. If the element complies than sufunction returns an empty string. Otherwise it returns a message describing the error and highlight appropriate element with yellow.<br /><br />function validateFormOnSubmit(theForm) {<br />var reason = "";<br /><br /> reason += validateUsername(theForm.username);<br /> reason += validatePassword(theForm.pwd);<br /> reason += validateEmail(theForm.email);<br /> reason += validatePhone(theForm.phone);<br /> reason += validateEmpty(theForm.from);<br /> <br /> if (reason != "") {<br /> alert("Some fields need correction:\n" + reason);<br /> return false;<br /> }<br /><br /> return true;<br />}jainmaneesh12@gmail.comhttp://www.blogger.com/profile/11433144482700682713noreply@blogger.com