0

I have a script running on sites. When I'm trying to generate unique id, like suggests in this question, it sometime fails to do so because it seems like the sites are overwrite the functions Date.getTime and Math.Random. Is there a way to generate a unique id in this cases?

Note: I need to use the unique in the server side

Community
  • 1
  • 1
Alon Ashkenazi
  • 1,223
  • 4
  • 21
  • 29
  • *"the sites are overwrite the functions Date.getTime and Math.Random"* ? Really ? – Denys Séguret Jun 25 '13 at 16:18
  • What kind of scripts are you speaking about ? Userscripts ? Or one of your libraries ? If you made a library, you probably should assume the basic environment hasn't been too much broken, this is the responsibility of the script user. – Denys Séguret Jun 25 '13 at 16:19
  • I'm talking about script that wrote, I'm not using any libraries in this script – Alon Ashkenazi Jun 25 '13 at 16:53
  • Based on `Math.Random`, it looks like you may be talking about Java, not JavaScript. Can you re-tag if this is the case? – Aaron Dufour Jun 25 '13 at 18:11
  • You should create your uuid's server side. Those are faking Math and Date (who else should overwrite this?) will also be able to fake any another approach. – jgb Jun 25 '13 at 18:59

2 Answers2

1

For client-side purposes the following should be enough:

var uniqueId = (function() {
    var id = 0;
    return function() {
        return id++;
    };
})();
Esailija
  • 138,174
  • 23
  • 272
  • 326
-3

Wow!

If your words are true and no way to fix code on sites: Add Jquery and call $.now() - it will return timestamp for you.

but I am 90% sure you do something wrong from scratch, reconsider your code on sites

Dimash
  • 581
  • 1
  • 5
  • 13
  • 1
    jQuery.now is using Date.getTime. now: function() { return ( new Date() ).getTime(); } – jgb Jun 25 '13 at 16:27