uniqid() in javascript/jquery?

前端 未结 8 1843
孤城傲影
孤城傲影 2021-01-02 09:58

what\'s the equivalent of this function in javascript:

http://php.net/manual/en/function.uniqid.php

Basically I need to generate a random ID that looks like:

相关标签:
8条回答
  • 2021-01-02 10:33

    Underscore.js has a uniqueid() method
    https://underscorejs.org/#uniqueId

    _.uniqueId([prefix])
    Generate a globally-unique id for client-side models or DOM elements that need one. If prefix is passed, the id will be appended to it.

    _.uniqueId('contact_');  
    => 'contact_104'
    
    0 讨论(0)
  • 2021-01-02 10:38

    Try this (Work in php).

    $prefix = chr(rand(97,121));  
    $uniqid =  $prefix.uniqid(); // $uniqid = uniqid($prefix);
    

    Try this for JavaScript::

    var n = Math.floor(Math.random() * 11);
    var k = Math.floor(Math.random() * 1000000);
    var m = String.fromCharCode(n) + k;
    
    0 讨论(0)
  • 2021-01-02 10:38

    Found this:

    https://github.com/makeable/uuid-v4.js/blob/master/uuid-v4.js

    and slightly modified to this:

    function uniqid(length){
      var dec2hex = [];
      for (var i=0; i<=15; i++) {
        dec2hex[i] = i.toString(16);
      }
    
      var uuid = '';
      for (var i=1; i<=36; i++) {
        if (i===9 || i===14 || i===19 || i===24) {
          uuid += '-';
        } else if (i===15) {
          uuid += 4;
        } else if (i===20) {
          uuid += dec2hex[(Math.random()*4|0 + 8)];
        } else {
          uuid += dec2hex[(Math.random()*16|0)];
        }
      }
    
      if(length) uuid = uuid.substring(0,length);
      return uuid;
    }
    

    works great.

    0 讨论(0)
  • 2021-01-02 10:40
    <html>
    <head>
    <script type="text/javascript">
    function generateSerial(len) {
        var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
        var string_length = 10;
        var randomstring = '';
    
        for (var x=0;x<string_length;x++) {
    
            var letterOrNumber = Math.floor(Math.random() * 2);
            if (letterOrNumber == 0) {
                var newNum = Math.floor(Math.random() * 9);
                randomstring += newNum;
            } else {
                var rnum = Math.floor(Math.random() * chars.length);
                randomstring += chars.substring(rnum,rnum+1);
            }
    
        }
        alert(randomstring);
    }
    generateSerial(8);
    </script>
    </head>
    <body>
    
    </body>
    </html>
    

    It's a bit convoluted, but you get the gist I'm sure!
    Example: http://jsfiddle.net/Ng4tB/

    0 讨论(0)
  • 2021-01-02 10:48

    All answers here (except phpjs) don't generate unique IDs because it's based on random. Random is not unique !

    a simple solution :

    window.unique_id_counter = 0 ;
    var uniqid = function(){
        var id ;
        while(true){
            window.unique_id_counter++ ;
            id = 'uids_myproject_' + window.unique_id_counter ;
            if(!document.getElementById(id)){
                /*you can remove the loop and getElementById check if you 
                  are sure that noone use your prefix and ids with this 
                  prefix are only generated with this function.*/
                return id ;
            }
        }
    }
    

    It's easy to add dynamic prefix if it's needed. Just change unique_id_counter into an array storing counters for each prefixes.

    0 讨论(0)
  • 2021-01-02 10:48

    The real question is, do you need the UUID to be RFC 4122 compliant? Your question seems to suggest you don't, so it wouldn't be too hard to create a function based simply on Math.random() to generate IDs like that. Plus it will be a lot faster than the phpJS implementation.

    0 讨论(0)
提交回复
热议问题