Just started learning JS, and been having an issue with a library i'm trying to import.
The developer recommends the following:
<script>
window.sodium = {
onload: function (sodium) {
let h = sodium.crypto_generichash(64, sodium.from_string('test'));
console.log(sodium.to_hex(h));
}
};
</script>
<script src="sodium.js" async></script>
However, this only executes when the page loads - I can't run the functions further down the page when they are required. As such:
<script>
var App = new function(myLibrary){
this.sodium = myLibrary;
this.example = function () {
h = this.sodium.crypto_generichash(64, this.sodium.from_string('test'));
return this.sodium.to_hex(h);
}
}
var create = new App(window.sodium);
</script>
/...../
<script>console.log(create.example());</script>
<script src="sodium.js" async></script>
How would I do this? I kept getting errors such as "sodium.crypto_generichash" is not a function. Or sodium is not defined.