Aaron Kc Hsu
Aaron Kc Hsu

Reputation: 19

How to use Javascript to call an external library's function inside of a function?

Hello so I am using the CryptoJS library to do a SHA256 hash. I am having the problem where it allows me to do something like this.

<script src="https://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha256.js"></script>

<script> var p = CryptoJS.sha256("password"); </script>

but not this

<script src="https://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha256.js"></script>

<script>
 function login() {
 var p = CryptoJS.sha256("password");
 } </script>

This gives me an undefined function error.

I am new to JavaScript... so I may just not be that familiar with scopes and external libraries could someone help clarify this for me?

Upvotes: 0

Views: 1451

Answers (1)

AlexGM
AlexGM

Reputation: 168

If you call the function login() as you have it written all that will happen is that a hash of the word "password" will be generated and assigned to the variable p. -- and that is all!

If you want a working function you need to do something with this variable p. You could start by looking at what is being generated by adding console.log(p) inside the function and taking a look at your console.

And if all you are looking for is the hash of "password" you can return p.

Good luck!

Upvotes: 1

Related Questions