Aug
08

Detect enter key pressed

JavaScript          Trackback

Do you want to detect if somebody pressed enter on your form (maybe to prevent form submitting when someone press enter key). Here is jquery code that can help you to detect enter key being pressed:


$('input').keyup(function(e) {
if(e.keyCode == 13) {
alert('Enter key was pressed.');
}
});


Dec
05

Disable Enter key in the form using JavaScript

JavaScript          Trackback

Have an HTML form on your site and sometimes you are receiving incomplete submissions? Ever wonder why? I’ll tell you. Many times your visitors are hitting the ‘Enter’ key instead of the ‘Tab’ key, and pressing ‘Enter’ button is not taking them to next field but submits the form, and that is causing incomplete form submissions.
Place this JavaScript snippet between your HEAD tags:
Use this simple JavaScript snippet, put it before tag, and that should disable the ‘Enter’ key within your form.
[sourcecode language='javascript']
<script type=”text/javascript”> function noEnter(e) { var e=(e)?e:((event)?event:null); var field=(e.target)?e.target:((e.srcElement)?e.srcElement:null); if ((e.keyCode==13)&&(field.type==”text”)) {return false;} } document.onkeypress = noEnter; </script>
[/code]


Sep
10

Javascript leading zero string prototype

JavaScript          Trackback

When you are working with dates it is useful to have all dates formatted in a same way. For example, it is better to have all dates formatted like ‘2007-01-01′ then like ‘2007-1-1′. This is javascript prototype that can help you with this leading zeros:


<script type="text/javascript">
String.prototype.leadingZero = function(){
if (this.length == 1) return '0' + this; else return this;
}
//show how it works
test1 = '1';  // we need zero here
test2 = '02'; // don't need zero

document.write ('"' + test1.leadingZero() + '"<br>');
document.write ('"' + test2.leadingZero() + '"<br>');
</script>

</body>
</html>

Sep
10

Detect different browsers with Javascript

JavaScript          Trackback

If you are working with JavaScript you were probably in situation that your script is working with some browsers and not working with others. It would help to know which browser is user using. Here is table with some object detection code which can help you to determine that:

Scheme Description
document.getElementById Detects IE5+, Firefox1+, and Opera7+ (modern browsers in general)
window.getComputedStyle Detects Firefox1+ and Opera 8+
Array.every Detects Firefox1.5+ (method detection)
window.Iterator Detects Firefox2+
document.all Detects IE4+
window.attachEvent Detects IE5+
window.createPopup Detects IE5.5+
document.compatMode && document.all Detects IE6+
window.XMLHttpRequest Detects IE7, Firefox1+, and Opera8+
document.documentElement && typeof document.documentElement.style.maxHeight!=”undefined” Detects IE7
window.opera Detects Opera (any version).

You can use this lines of code to detect specific browser:

<script type=”text/javascript”>

if (document.getElementById) alert(”Modern browser detected”)

if (window.getComputedStyle) alert(”Firefox1+ or Opera 8+ detected”)

if (Array.every) alert(”Firefox1.5+ detected”)

if (window.Iterator) alert(”Firefox2+ detected”)

if (document.all) alert(”IE4+ detected”)

if (window.attachEvent) alert(”IE5+ detected”)

if (window.createPopup) alert(”IE5.5+ detected”)

if (document.compatMode && document.all) alert(”IE7, Firefox1+, and Opera8+ detected”)

if (window.XMLHttpRequest) alert(”IE6 detected”)

if (document.documentElement && typeof document.documentElement.style.maxHeight!=”undefined”) alert(”IE7 detected”)

if (window.attachEvent) alert(”Opera (any version) detected”)

</script>


top