I know how to do that in node.js, with npm-managed libraries like uuid
. But what's a decent way to do this in browser-based SAP UI5 JavaScript code?
Asked
Active
Viewed 3,866 times
3

Boghyon Hoffmann
- 17,103
- 12
- 72
- 170

Florian
- 4,821
- 2
- 19
- 44
-
FYI: `globalThis.crypto.randomUUID()` is now available in all major browsers. Updated my answer accordingly – Boghyon Hoffmann Apr 17 '22 at 23:01
1 Answers
5
Generating UUID in plain JavaScript
const myUniversallyUniqueID = globalThis.crypto.randomUUID();
console.log(myUniversallyUniqueID); // e.g.: "a38aa6d5-bf7e-4caa-afe1-0144507e215c"
crypto.randomUUID
is now supported by all major JS engines. I.e. globalThis.crypto.randomUUID()
can be used in browsers as well as in Deno / Node.js.
Generating UID in SAPUI5
Not a UUID, but UI5 provides the function module sap/base/util/uid
which returns a string that is unique within the same JavaScript context (I.e. it is very possible to generate the exact same UIDs when two iframes run uid()
at the same time).
const myUniqueID = uid(); // uid required from "sap/base/util/uid"
console.log(myUniqueID); // e.g.: "id-1650235945092-39"
The implementation is fairly simple and easy to understand.

Boghyon Hoffmann
- 17,103
- 12
- 72
- 170