为每个设备生成唯一ID

时间:2022-11-25 11:25:52

I want to generate a unique id for each device. Currently I am using fingerprint.js for this. My code is:

我想为每个设备生成一个唯一的ID。目前我正在使用fingerprint.js。我的代码是:

var fingerprint = new Fingerprint().get();

But I want to generate unique id with out using any plugins. Can any one help me please?

但我希望使用任何插件生成唯一的ID。有人可以帮帮我吗?

4 个解决方案

#1


7  

Friends,

朋友们,

At last I found the answer. This code will generate unique id for each device(in a browser) all the time. uid is the generated unique id.

最后我找到了答案。此代码将始终为每个设备(在浏览器中)生成唯一ID。 uid是生成的唯一ID。

var navigator_info = window.navigator;
var screen_info = window.screen;
var uid = navigator_info.mimeTypes.length;
uid += navigator_info.userAgent.replace(/\D+/g, '');
uid += navigator_info.plugins.length;
uid += screen_info.height || '';
uid += screen_info.width || '';
uid += screen_info.pixelDepth || '';
console.log(uid);

Thank you all for supporting me.

谢谢大家的支持。

#2


3  

For example like this:

例如这样:

function generateUUID(){
    var d = new Date().getTime();
    var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        var r = (d + Math.random()*16)%16 | 0;
        d = Math.floor(d/16);
        return (c=='x' ? r : (r&0x3|0x8)).toString(16);
    });
    return uuid;
};

More on the topic: Create GUID / UUID in JavaScript?

有关该主题的更多信息:在JavaScript中创建GUID / UUID?

Edit: In your comment you say, you want to generate the same id per device at any time. For such tasks, building hashes is a way to go. Get any property / properties of your device, which are unique for this device (whatever it is, it is difficult to say without example). Than build a hash out of them, for example:

编辑:在您的评论中,您想要随时为每台设备生成相同的ID。对于这样的任务,建立哈希是一种方法。获取您设备的任何属性/属性,这些属性对于此设备是唯一的(无论它是什么,如果没有示例,很难说)。比从中构建一个哈希,例如:

var uniqueId = someHashFunction(device.property1 + device.property2 + ...);

There are plenty of hashing functions on the internet, as an example you can have a look at this one: http://phpjs.org/functions/md5/ This will return a unique value for given properties.

互联网上有很多散列函数,例如你可以看看这个函数:http://phpjs.org/functions/md5/这将返回给定属性的唯一值。

#3


1  

You can use timestamp for a unique ID and append some static text to it:

您可以使用时间戳作为唯一ID,并为其附加一些静态文本:

var uniqueID = "ID-"+(new Date()).getTime().toString();

#4


0  

The easiest way is

最简单的方法是

var uuid = new Date().getTime().toString();

#1


7  

Friends,

朋友们,

At last I found the answer. This code will generate unique id for each device(in a browser) all the time. uid is the generated unique id.

最后我找到了答案。此代码将始终为每个设备(在浏览器中)生成唯一ID。 uid是生成的唯一ID。

var navigator_info = window.navigator;
var screen_info = window.screen;
var uid = navigator_info.mimeTypes.length;
uid += navigator_info.userAgent.replace(/\D+/g, '');
uid += navigator_info.plugins.length;
uid += screen_info.height || '';
uid += screen_info.width || '';
uid += screen_info.pixelDepth || '';
console.log(uid);

Thank you all for supporting me.

谢谢大家的支持。

#2


3  

For example like this:

例如这样:

function generateUUID(){
    var d = new Date().getTime();
    var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        var r = (d + Math.random()*16)%16 | 0;
        d = Math.floor(d/16);
        return (c=='x' ? r : (r&0x3|0x8)).toString(16);
    });
    return uuid;
};

More on the topic: Create GUID / UUID in JavaScript?

有关该主题的更多信息:在JavaScript中创建GUID / UUID?

Edit: In your comment you say, you want to generate the same id per device at any time. For such tasks, building hashes is a way to go. Get any property / properties of your device, which are unique for this device (whatever it is, it is difficult to say without example). Than build a hash out of them, for example:

编辑:在您的评论中,您想要随时为每台设备生成相同的ID。对于这样的任务,建立哈希是一种方法。获取您设备的任何属性/属性,这些属性对于此设备是唯一的(无论它是什么,如果没有示例,很难说)。比从中构建一个哈希,例如:

var uniqueId = someHashFunction(device.property1 + device.property2 + ...);

There are plenty of hashing functions on the internet, as an example you can have a look at this one: http://phpjs.org/functions/md5/ This will return a unique value for given properties.

互联网上有很多散列函数,例如你可以看看这个函数:http://phpjs.org/functions/md5/这将返回给定属性的唯一值。

#3


1  

You can use timestamp for a unique ID and append some static text to it:

您可以使用时间戳作为唯一ID,并为其附加一些静态文本:

var uniqueID = "ID-"+(new Date()).getTime().toString();

#4


0  

The easiest way is

最简单的方法是

var uuid = new Date().getTime().toString();