uniqid() in javascript/jquery?

2019-02-17 02:32发布

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: a4245f54345 and starts with a alphabetic character (so I can use it as a CSS id)

7条回答
男人必须洒脱
2楼-- · 2019-02-17 03:03

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.

查看更多
冷血范
3楼-- · 2019-02-17 03:04

I have been using this ...

I use it exactly as I would if it where PHP. Both return the same result.

function uniqid(a = "",b = false){
    var c = Date.now()/1000;
    var d = c.toString(16).split(".").join("");
    while(d.length < 14){
        d += "0";
    }
    var e = "";
    if(b){
        e = ".";
        var f = Math.round(Math.random()*100000000);
        e += f;
    }
    return a + d + e;
}
查看更多
时光不老,我们不散
4楼-- · 2019-02-17 03:05
<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/

查看更多
SAY GOODBYE
5楼-- · 2019-02-17 03:08

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;
查看更多
6楼-- · 2019-02-17 03:08
function uniqId(prefix) {
    if (window.performance) {
        var s = performance.timing.navigationStart;
        var n = performance.now();
        var base = Math.floor((s + Math.floor(n))/1000);
    } else {
        var n = new Date().getTime();
        var base = Math.floor(n/1000);
    }   
    var ext = Math.floor(n%1000*1000);
    var now = ("00000000"+base.toString(16)).slice(-8)+("000000"+ext.toString(16)).slice(-5);
    if (now <= window.my_las_uid) {
        now = (parseInt(window.my_las_uid?window.my_las_uid:now, 16)+1).toString(16);
    }
    window.my_las_uid = now;
    return (prefix?prefix:'')+now;
}

it is generated on "the same" priciple as PHP's uniqId() - specifically encoded time in microseconds.

查看更多
Luminary・发光体
7楼-- · 2019-02-17 03:12

Try PHPJS's website, specifically the page for uniqid

查看更多
登录 后发表回答