Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught ReferenceError: <function> is not defined at HTMLInputElement.onclick

Tags:

javascript

I am trying to call a simple javascript function from my html page. The javascript function will decrypt using the "lib.js" file and the same is alerted.

Uncaught ReferenceError: decryptfun is not defined at HTMLInputElement.onclick (test.html:18)

The below is the only file I use (along with other dependent library files).

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="lib.js">
function decryptfun() {
    var pass = "hjubjbjhdgyuwj";
    var encrtoken = "abcdefghijklmn";

    var p = lib.decrypt(encrtoken, atob(pass));
    }
    alert(p);
</script>
</head>

<body>
<h1>Decrypt Operation</h1>
<input type="button" onclick="decryptfun()" value="Click">
</body>
</html>

I tried other suggestions provided for the same type of issue at Stackoverflow but I was not successful. Can anybody help me out in locating the cause of the issue ?

like image 589
mbvee Avatar asked Apr 04 '17 14:04

mbvee


People also ask

Is not a function Javascript onclick?

onclick is not a function" error occurs, because there is no onclick() function in jQuery. To solve the error, use the click function instead, e.g. $('#btn'). click(function () {} . Copied!

What is uncaught ReferenceError in Javascript?

The Javascript ReferenceError occurs when referencing a variable that does not exist or has not yet been initialized in the current scope. Reference errors in Javascript are of a few different types, with variations of each, that may get triggered in code.


1 Answers

Your error is because you have defined your function inside:

<script type="text/javascript" src="lib.js">

the correct way is to close that script tag first like so:

<script type="text/javascript" src="lib.js"></script>

and then defining the script tag again to define the function like so:

<script>
function(){
    //body of function
};
</script>

<script type="text/javascript" src="lib.js"></script>
<script>
  function decryptfun() {
    var pass = "hjubjbjhdgyuwj";
    var encrtoken = "abcdefghijklmn";

    //var p = lib.decrypt(encrtoken, atob(pass)); //USE THIS IN YOUR CASE
    var p = "test"; //just for example
    alert(p);
  }
</script>
<h1>Decrypt Operation</h1>
<input type="button" onclick="decryptfun()" value="Click">
like image 138
Shakti Phartiyal Avatar answered Sep 19 '22 21:09

Shakti Phartiyal