I have the following code embedded in my HTML:
<script>
// dec2hex :: Integer -> String
function dec2hex (dec) {
return ('0' + dec.toString(16)).substr(-2)
}
// generateId :: Integer -> String
function generateId (len) {
var arr = new Uint8Array((len || 40) / 2)
window.crypto.getRandomValues(arr)
return Array.from(arr, dec2hex).join('')
}
document.write(generateId(20))
</script>
Everything works fine but I would like to have all the javascript together in a ".js" file
My question is how to move to a ".js" file and return the result to HTML?
I did the following, I created a "test.js" with:
// dec2hex :: Integer -> String
function dec2hex (dec) {
return ('0' + dec.toString(16)).substr(-2)
}
// generateId :: Integer -> String
function generateId (len) {
var arr = new Uint8Array((len || 40) / 2)
window.crypto.getRandomValues(arr)
return Array.from(arr, dec2hex).join('')
}
And in HTML:
<!DOCTYPE html>
<html>
<head>
<script src="js/teste.js"></script>
<meta charset="utf-8">
<title>Random String Generator</title>
</head>
<body>
<p>generateId: id="generateId(20)"</p>
</body>
</html>
It does not work. Can anyone help this noob?