JavaScript FAQ
Ask me no questions and I’ll tell you no lies.
As this list becomes more comprehensive, we will try to add some order to it.
FAQ Topics
Strings and numbers
Q: How do I convert a string to a number?
A: You use parseInt(stringInstnace) or parseFloat(stringInstance)
var a = “5″; //this is a string, not a number var b = parseInt(a); //this converts the string into a number var c = a + a; //c will be a string with the value “55″; var d = b + b; //d will be a number with the value 10; alert(”c: ” + c + “nd: ” + d);
Q: How do I format a number so it only has N decimal places?
A: By using the .toFixed(N) method of the Number? object.
Example:
var num = 3.324234 alert(num.toFixed(2)); //will return 3.32
Q: Why does stringObj.replace("someString", "replacementString"); only replace the first occurrence of "someString"?
A: Becaues the first argument for tring.replace method is a Regular Expression? not a string. If you do the following it’ll replace all occurrences of "someString": stringObj.replace(/someString/g, "replacementString");.
Detection
Q: How do I test if Javascript is enabled?
A:The simple answer is that if Javascript is enabled, your code will work, and there is no way to ‘test’ per say. However, you can inform a user who has Javascript disabled by using the noscript tag.
noscriptThis site requires Javascript and you currently have it dissabled. noscript
Another way to ensure that your site is only viewed by people that have js enabled, is to have your index page do a redirect to the real page. If they do not have js enabled, the page will not redirect and you can have the index say something like ‘js must be enabled’.
location.href=’realStartPage.htm’; js not enabled
Q: How do I test if cookies are enabled?
A: By attempting to write a cookie and then reading it.
var testcookie = ‘jscookietest=valid’; document.cookie = testcookie; if (document.cookie.indexOf(testcookie) == -1) { alert(”You do not have cookies enabled. This site requires cookies to be enabled.”);} document.cookie = testcookie + ‘;expires=’ + new Date(0).toGMTString();
Q: How do I test if ActiveX is enabled?
A: By using the following snippet:
var agnt = navigator.userAgent.toLowerCase(); if (agnt.indexOf(’msie’)!= -1 && document.all) { var control = (agnt.indexOf(’msie 5′) != -1) ? ‘Microsoft.XMLHTTP’ : ‘Msxml2.XMLHTTP’; try {new ActiveXObject(control);} catch (e) { alert(”You do not have ActiveX enabled.”); else { alert(“ActiveX is currently not enabled. You’re smarter than the average bear.”;}
Q: How do know which browser the client is using?
A: User agent sniffing results in cruddy applications, you should try to use object detection where possible instead. For example, if you want to see whether element.currentStyle (an IE attribute) will be available, don’t try and work out if you’ve got IE, just see if you’ve got element.currentStyle
Forms
Q: Can I submit a form using Javascript?
A: Yes.
document.formName.submit();
Q: Can I change the action of a form with Javascript?
A: Yes.
document.formName.action = “somePage.jsp”;
Q: Can I validate a form before submitting it?
A: Yes, by attaching an onsubmit event to the