generate random string for div id

后端 未结 12 737
旧时难觅i
旧时难觅i 2020-12-04 17:03

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 toget

12条回答
  •  被撕碎了的回忆
    2020-12-04 17:54

    window.btoa(String.fromCharCode(...window.crypto.getRandomValues(new Uint8Array(5))))

    Using characters except ASCII letters, digits, '_', '-' and '.' may cause compatibility problems, as they weren't allowed in HTML 4. Though this restriction has been lifted in HTML5, an ID should start with a letter for compatibility.

提交回复
热议问题