最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ReferenceError: CryptoJs is not defined - Stack Overflow

programmeradmin6浏览0评论

I tried to hash a text in client-side. I used following code to hash it, but it shows this Reference Error.

<html>

<head>
  <script src=".1.9-1/md5.js">
  </script>
</head>

<body>
  <script>
    var plaintext = "hiii";
    var encrptedText = CryptoJs.md5(plaintext);
    alert("Encrpted Text : " + encrptedText.toString());
  </script>
</body>

</html>

I tried to hash a text in client-side. I used following code to hash it, but it shows this Reference Error.

<html>

<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/md5.js">
  </script>
</head>

<body>
  <script>
    var plaintext = "hiii";
    var encrptedText = CryptoJs.md5(plaintext);
    alert("Encrpted Text : " + encrptedText.toString());
  </script>
</body>

</html>

Share Improve this question edited Dec 28, 2018 at 10:21 Sebastian Simon 19.5k8 gold badges60 silver badges84 bronze badges asked Dec 28, 2018 at 10:09 krishnakrishna 1461 gold badge2 silver badges11 bronze badges 3
  • There’s an error in the JS file itself. – Sebastian Simon Commented Dec 28, 2018 at 10:21
  • 2 the variable is named CryptoJS not CryptoJs – awd Commented Dec 28, 2018 at 10:24
  • If you try to evaluate the script in the browser console, you get the following error: Uncaught TypeError: Cannot read property 'lib' of undefined – Francesco Pezzella Commented Dec 28, 2018 at 10:24
Add a comment  | 

2 Answers 2

Reset to default 16

Use the entire package - not just the md5 module - change the src in your script tag

<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.js"></script></head>
<body>
<script>
var plaintext="hiii";
var encrptedText = CryptoJS.MD5(plaintext)
alert("Encrpted Text : "+ encrptedText.toString());
</script>
 </body>
</html>

If for you important the size of extended libraries, that you can use pure-md5 (4.76kb) instead crypto-js (187.44kb).

<html>

<head>
<script src="https://unpkg.com/pure-md5@latest/lib/index.js">
  </script>
</head>

<body>
  <script>
    var plaintext = "hiii";
    var encrptedText = md5(plaintext);
    alert("Encrpted Text : " + encrptedText.toString());
  </script>
</body>

</html>

发布评论

评论列表(0)

  1. 暂无评论