Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generate random string for div id

Tags:

javascript

I want to display YouTube videos on my website, but I need to be able to add a unique id for each video that's going to be shared by users. So I put this together, and I have run into a little problem. I am trying to get the JavaScript to add a random string for the div id, but it's not working, showing the string:

<script type='text/javascript' src='jwplayer.js'></script> <script type='text/javascript'> function randomString(length) {     var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz'.split('');      if (! length) {         length = Math.floor(Math.random() * chars.length);     }      var str = '';     for (var i = 0; i < length; i++) {         str += chars[Math.floor(Math.random() * chars.length)];     }     return str; }  var div = randomString(8); </script>  <div id='div()'>This text will be replaced</div>  <script type='text/javascript'>   jwplayer('div()').setup({     'flashplayer': 'player.swf',     'file': 'http://www.youtube.com/watch?v=4AX0bi9GXXY',     'controlbar': 'bottom',     'width': '470',     'height': '320'   }); </script> 
like image 638
sarsar Avatar asked Jul 28 '11 14:07

sarsar


1 Answers

I really like this function:

function guidGenerator() {     var S4 = function() {        return (((1+Math.random())*0x10000)|0).toString(16).substring(1);     };     return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4()); } 

From Create GUID / UUID in JavaScript?

like image 53
Joe Avatar answered Oct 09 '22 03:10

Joe