Javascript 未捕获的 ReferenceError:<function> 未在 HTMLInputElement.onclick 中定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43209534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:45:05  来源:igfitidea点击:

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

javascript

提问by mbvee

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.

我正在尝试从我的 html 页面调用一个简单的 javascript 函数。javascript 函数将使用“lib.js”文件解密,并发出相同的警报。

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

未捕获的 ReferenceError:decryptfun 未在 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 ?

我在 Stackoverflow 上尝试了针对同一类型问题提供的其他建议,但没有成功。有人可以帮我找出问题的原因吗?

回答by Shakti Phartiyal

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">