[英]Uncaught ReferenceError: <function> is not defined at HTMLInputElement.onclick
I am trying to call a simple javascript function from my html page.我正在尝试从我的 html 页面调用一个简单的 javascript 函数。 The javascript function will decrypt using the "lib.js" file and the same is alerted.
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.我在 Stackoverflow 上尝试了针对同一类型问题提供的其他建议,但没有成功。 Can anybody help me out in locating the cause of the issue ?
有人可以帮我找出问题的原因吗?
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">
My problem was the I was defining my function inside我的问题是我在里面定义了我的函数
<script>
$(document).ready(function () {
function myFunction() ....
})
</script>
It worked fine when directly defined in the script
tag在
script
标签中直接定义时它工作正常
<script>
function myFunction() ....
</script>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Person.aspx.cs" Inherits="JqueryDemo1.Person" %>
<!DOCTYPE html>`enter code here`
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="Scripts/jquery-3.5.1.min.js"></script>
<script type="text/javascript">
function SubmitData() {
$.ajax({
url: 'Person.aspx/InsertData',
type: 'post',
contentType: 'application/json;charset=utf-8',
dataType: 'json',
data: "{Name:'" + $('#txtname').val() + "', Address:'" + $('#txtaddress').val() + "', Age:'" + $('#txtage').val() + "'}",
success: function () {
alert("Data inserted successfully");
},
error: function () {
alert("insert errorr");
}
});
</script>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>Name:</td>
<td>
<input type="text" id="txtname" /></td>
</tr>
<tr>
<td>Address:</td>
<td>
<input type="text" id="txtaddress" /></td>
</tr>
<tr>
<td>Age:</td>
<td>
<input type="text" id="txtage" /></td>
</tr>
<tr>
<td>
<input type="button" id="btnsave" value="Submit" onclick="SubmitData()" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.