Javascript Generate Uuid Number Tutorial


Share:
Published By: Admin, Published On: Jul 13, 2021, Category: Javascript
Hello Friends, Now let's see example of how to use javascript generate uuid number example. We will use how to use if generate uuid number in javascript. Here you will learn how to use javascript generate uuid number. This is a short guide on generate uuid number. Let's get started with how to generate uuid number in javascript. Here i will give you many example how you can check javascript generate uuid number. Example: 1

<html>
    <head>
        <meta charset="utf-8">
        <title>Write a JavaScript function to create a UUID</title>
    </head>
    <body>
        <script type="text/javascript">
            function create_UUID(){
                var dt = new Date().getTime();
                var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
                    var r = (dt + Math.random()*16)%16 | 0;
                    dt = Math.floor(dt/16);
                    return (c=='x' ? r :(r&0x3|0x8)).toString(16);
                });
                return uuid;
            }
            console.log(create_UUID());
        </script>
    </body>
</html>
Output:
4f605a81-3bfa-45db-8cc2-1be9187a461b
Example: 2
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Javascript Generate Uuid Number</title>
        <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    </head>
    <body class="bg-dark">
        <div class="container mt-5">
            <div class="row">
                <div class="col-md-6 offset-md-3">
                    <div class="card">
                        <div class="card-header">
                            <h5>Javascript Generate Uuid Number</h5>
                        </div>
                        <div class="card-body">
                            <p id="uuid"></p>
                            <button id="generateUUID" class="btn btn-primary" onclick="onClick();">Generate UUID</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <script type="text/javascript">
            function generateUUID() { // Public Domain/MIT
                var d = new Date().getTime();//Timestamp
                var d2 = (performance && performance.now && (performance.now()*1000)) || 0;
                return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
                    var r = Math.random() * 16;//random number between 0 and 16
                    if(d > 0){//Use timestamp until depleted
                        r = (d + r)%16 | 0;
                        d = Math.floor(d/16);
                    } else {
                        r = (d2 + r)%16 | 0;
                        d2 = Math.floor(d2/16);
                    }
                    return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
                });
            }
            var onClick = function(){
                document.getElementById('uuid').textContent = generateUUID();
            }
            onClick();
        </script>
    </body>
</html>
Output:
b0d88071-88b3-4ae2-bfa0-9fbdb96ca5e4

It will help you....

Share: