-2

I need a javascript which make Unique Random DIV ID. Currently i am using it with PHP


<?php 
function getRandomWord($len = 10) {
    $word = array_merge(range('a', 'z'), range('A', 'Z'));
    shuffle($word);
    return substr(implode($word), 0, $len);
}
$divid = getRandomWord();
$classid = getRandomWord();
?>

div = <?php echo  $divid; ?>
class = <?php echo  $classid; ?>

<div id="<?php echo  $divid; ?>" class="<?php echo  $classid; ?>"> </div>

The Above PHP code are working fine and provide a unique ID for DIV Randomly,

How to do same with javascript Thank You.

  • 2
    Please describe your precise requirements. How long should the ID be? What characters do you want in it? How random does it need to be? Are you just looking for a function that generates a random string or are you also asking for code to insert a div into the DOM with that id assigned. Does the id just need to be unique in the current page? Or unique in the world? What have you researched and tried so far? – jfriend00 Apr 05 '16 at 06:18
  • 1
    Around 5 - 10 characters and The Letter a - z and A - Z. – Rajeev Ranjan Sharma Apr 05 '16 at 06:20
  • 2
    FYI, `Math.random().toString()` will make a pretty random string of digits. If you just want it unique within the current page, you can just use a monotonically increasing counter. – jfriend00 Apr 05 '16 at 06:20
  • 1
    Why does it have to even have alpha characters in it. If you just want random, then why not just `Math.random().toString()` and be done with it. – jfriend00 Apr 05 '16 at 06:20
  • 1
    I need in alphabet – Rajeev Ranjan Sharma Apr 05 '16 at 06:25
  • 1
    Why do you need in alphabet? A div ID can be digits. – jfriend00 Apr 05 '16 at 06:26
  • It is better to do this with php only, considering performance. – Mr_Green Apr 05 '16 at 07:19
  • 1
    @Mr_Green .html extension not support PHP code and also PHP code file cannot called in different server location. That why i need in javascript – Rajeev Ranjan Sharma Apr 05 '16 at 07:21
  • @RajeevRanjanSharma here is a link... http://stackoverflow.com/a/27872144/3568847 – Praveen Kumar Apr 05 '16 at 07:37

1 Answers1

3

Here's a simple random string generator in a working snippet so you can see what it generates:

function makeRandomStr(len) {
  var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  var result = "", rand;
  for (var i = 0; i < len; i++) {
    rand = Math.floor(Math.random() * chars.length);
    result += chars.charAt(rand);
  }
  return result;
}

document.getElementById("run").addEventListener("click", function() {
  var div = document.createElement("div");
  div.innerHTML = makeRandomStr(40);
  document.getElementById("results").appendChild(div);
});
<button id="run">Make Random</button><br><br>
<pre id="results"></pre>

The ONLY way to guarantee perfectly unique in the world would be to have a central server that was keeping track of all previously allocated strings so no duplicate could ever be used. But since you did not specify how unique it needed to be, I offered a random string. If you make it longer, your odds of conflict go to near zero.


You can also add other environmental elements such as timestamp, mouse positions, etc... that make it even less likely to ever conflict.

For example, here a timestamp is added to the end of the string so you'd not only have to accidentally generate the same random string, but you'd also have to do it at exactly the same clock time.

function makeRandomStr(len) {
  var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  var result = "", rand;
  for (var i = 0; i < len; i++) {
    rand = Math.floor(Math.random() * chars.length);
    result += chars.charAt(rand);
  }
  return result + "_" + Date.now();
}

document.getElementById("run").addEventListener("click", function() {
  var div = document.createElement("div");
  div.innerHTML = makeRandomStr(40);
  document.getElementById("results").appendChild(div);
});
<button id="run">Make Random</button><br><br>
<pre id="results"></pre>
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • But this can't be unique always! – Pugazh Apr 05 '16 at 06:31
  • @Pugazh - It's random. If you want unique in the world, the only way to guarantee that is to have a server keep track of all previously allocated strings to make sure you never accidentally allocate the same one twice. The OP did not specify any sort of randomness or uniqueness criteria. This can be made more unique by adding timestamp elements, and other environmental factors, etc... as required. – jfriend00 Apr 05 '16 at 06:33
  • I understand. But the question states `Unique Random DIV ID` and so i suggested that comment. – Pugazh Apr 05 '16 at 06:35
  • @Pugazh - The OP offers no meaningful specification for what "unique" actually means in their case, even after I asked. It may only need to be unique in the page in which case a simple monotonically increasing counter is all that is needed. – jfriend00 Apr 05 '16 at 06:36
  • Yeah true. I just thought to suggest a increasing counter. Thanks!! – Pugazh Apr 05 '16 at 06:38