[Edited 2021-10-16 to reflect latest best-practices for producing RFC4122-complaint UUIDs]
Most readers here will want to use the uuid
module. It is well-tested and supported.
The crypto.randomUUID()
function is an emerging standard that is supported in Node.js
and an increasing number of browsers.
If neither of those work for you, there is this method (based on the original answer to this question):
function uuidv4() {
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
);
}
console.log(uuidv4());
Note: The use of any UUID generator that relies on Math.random() is strongly discouraged (including snippets featured in previous versions of this answer) for reasons best-explained here. TL;DR: Math.random()-based solutions do not provide good uniqueness guarantees.
Short & Snazzy:
+ new Date()
A unary operator like plus
triggers the valueOf
method in the Date
object and it returns the timestamp (without any alteration).
Details:
On almost all current browsers you can use Date.now()
to get the UTC timestamp in milliseconds; a notable exception to this is IE8 and earlier (see compatibility table).
You can easily make a shim for this, though:
if (!Date.now) {
Date.now = function() { return new Date().getTime(); }
}
To get the timestamp in seconds, you can use:
Math.floor(Date.now() / 1000)
Or alternatively you could use:
Date.now() / 1000 | 0
Which should be slightly faster, but also less readable.
(also see this answer or this with further explaination to bitwise operators).
I would recommend using Date.now()
(with compatibility shim). It's slightly better because it's shorter & doesn't create a new Date
object. However, if you don't want a shim & maximum compatibility, you could use the "old" method to get the timestamp in milliseconds:
new Date().getTime()
Which you can then convert to seconds like this:
Math.round(new Date().getTime()/1000)
And you can also use the valueOf
method which we showed above:
new Date().valueOf()
Timestamp in Milliseconds
var timeStampInMs = window.performance && window.performance.now && window.performance.timing && window.performance.timing.navigationStart ? window.performance.now() + window.performance.timing.navigationStart : Date.now();
console.log(timeStampInMs, Date.now());
Best Solution
The
TEXTAREA
tag does not have aMAXLENGTH
attribute the way that anINPUT
tag does, at least not in most standard browsers. A very simple and effective way to limit the number of characters that can be typed into aTEXTAREA
tag is:Note:
onKeyPress
, is going to prevent any button press, any button including the backspace key.This works because the Boolean expression compares the field's length before the new character is added to the maximum length you want (50 in this example, use your own here), and returns true if there is room for one more,
false
if not. Returning false from most events cancels the default action. So if the current length is already 50 (or more), the handler returns false, theKeyPress
action is cancelled, and the character is not added.One fly in the ointment is the possibility of pasting into a
TEXTAREA
, which does not cause theKeyPress
event to fire, circumventing this check. Internet Explorer 5+ contains anonPaste
event whose handler can contain the check. However, note that you must also take into account how many characters are waiting in the clipboard to know if the total is going to take you over the limit or not. Fortunately, IE also contains a clipboard object from the window object.1 Thus:Again, the
onPaste
event andclipboardData
object are IE 5+ only. For a cross-browser solution, you will just have to use anOnChange
orOnBlur
handler to check the length, and handle it however you want (truncate the value silently, notify the user, etc.). Unfortunately, this doesn't catch the error as it's happening, only when the user attempts to leave the field, which is not quite as friendly.Source
Also, there is another way here, including a finished script you could include in your page:
http://cf-bill.blogspot.com/2005/05/textarea-maxlength-revisited.html