Submitting a form when the Enter key is pressed using Javascript
To implement this functionality, use the key event listener. The code for the Enter key is 13. function checkKey(e) { var key; if (window.event) { key = window.event.keyCode; } else { key = e.keyCode;...
View ArticleTracking movements of the mouse in Javascript
To get the position of the mouse, clientY and clientX properties are used in Internet Explorer. pageX and pagey properties in for all other browsers. Here is a complete listing showing the current...
View ArticleAdd a method to an array to perform calculations
Array.method('reduce', function <a style="text-decoration: none;color: inherit;cursor: default" href="http://paydayloansonlinecaus.com/">online payday loans direct lenders</a> (f, <a...
View ArticleCreate an array of 10 zeroes by adding a method to Javascript array
edToolbar() Array.dim = function (dimension, initial) { var a = [], i; for (i = 0; i < dimension; i += 1) { a[i] = initial; } return a; }; // Create an array of 10 zeroes <pre lang='javascr...
View ArticleCreate a 4×4 multidimensional array in Javascript
Array.matrix = function (m, n, initial) { var a, i, j, mat = []; for (i = 0; i < m; i += 1) { a = []; for (j = 0; j < n; j += 1) { a[j] = initial; } mat[i] = a; } return mat; }; var myMatrix =...
View ArticleExample for building a unit matrix in Javascript
edToolbar() Array.identity = function (n) { var i, mat = Array.matrix(n, n, 0); for (i = 0; i < n; i += 1) { mat[i][i] = 1; } return mat; }; myMatrix = Array.identity(<div><a...
View ArticleA regular expression to detect URLs in Javascript
edToolbar() var parse_url = /^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+) (?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/; var url = "http://www.w3mentor.com:80/someparts?q#fragment"; var...
View ArticleA regular expression that recognizes numbers in Javascript
Numbers consist of an integral part and optional minus sign, followed by an optional decimal part and an optional exponent. var parse_number = /^-?\d+(?:\.\d*)?(?:e[+\-]?\d+)?$/i; var test = function...
View ArticleGet the highest z-index in a page using javascript
Use the following javascript function to get the biggest ZIndex in a page if you need to add an element above all others. <script> function zin(){ var allElements =...
View ArticleGet current month in Javascript
var now = new Date() var month = now.getMonth() var ar = new Array(12) ar[0] = "January" ar[1] = "February" ar[2] = "March" ar[3] = "April" ar[4] = "May" ar[5] = "June" ar[6] = "July" ar[7] = "August"...
View ArticleGet name of day in week using Javascript
ar = new Array(7) ar[0] = " Sunday" ar[1] = " Monday" ar[2] = " Tuesday" ar[3] = " Wednesday" ar[4] = " Thursday" ar[5] = " Fri day" ar[6] = " Saturday" var someday = new Date(" January 3, 1987" ) var...
View ArticleGet time of day using Javascript
This script segment prints a short greeting based on the time of day. var now = new Date() var hour = now.getHours() var text = " " if (hour < 12) text = " morning" else if (hour < 16) text = "...
View ArticleExample of getMinutes() function in Javascript
The getMinutes() method returns the minute attribute of a Date instance.The integer returned is always from 0 to 59. var now = new Date() var minute = now.getMinutes() document.write(minute)Continue...
View ArticleUS Timezone information in Javascript
var now = new Date() var curOffset = now.getTimezoneOffset() curOffset /= 60 // convert from minutes to hours var zone = "" var prep = "" if (curOffset == 8) { zone = "west coast" prep = "on" } else if...
View ArticleGet the day of the current date last year using SetYear() in Javascript
var now = new Date() var year = now.getYear() now.setYear(year - 1) ar = new Array(7) ar[0] = " Sunday" ar[1] = " Monday" ar[2] = " Tuesday" ar[3] = " Wednesday" ar[4] = " Thursday" ar[5] = " Fri day"...
View ArticleGet the day on which the first day of the month using Javascript
var now = new Date() now.setDate(1) ar = new Array(7) ar[0] = "Sunday" ar[1] = "Monday" ar[2] = "Tuesday" ar[3] = "Wednesday" ar[4] = "Thursday" ar[5] = "Friday" ar[6] = "Saturday" document.write("The...
View ArticleExample of using toGMTString() in Javascript
var now = new Date() var ar1 = now.toGMTString().split(" ") document.write("The current time in Greenwich is " + ar1[4])Continue reading...
View ArticleExample of using setTimeout() in Javascript
The setTimeout() method evaluates an expression after a specified number of milliseconds have elapsed. <HTML> <HEAD> <TITLE>setTimeout() method</TITLE> <SCRIPT LANGUAGE =...
View ArticleUsing Javascript setTimeout() in recursive function
function alertNumbers(num) { if (num > 10) return alert(num) val = ++num timerID = setTimeout("alertNumbers(val)", 3000) } alertNumbers(0)Continue reading...
View ArticleGet the properties of the navigator object in Javascript
document.write(navigator.appName) document.write(navigator.appVersion) document.write(navigator.platform) document.write(navigator.cpuClass)Continue reading...
View Article