Jquery 如何在用户关闭网页时使用.ashx文件执行某些代码
我想在用户关闭网页时执行一些vb.net代码。我该怎么办。我有这个,但我不知道如何使用它。我在某处见过下面的说明,但我不能使用它: 可以将ajax调用绑定到beforeunload页面html事件。只需确保使用async false调用ajax,否则页面将在删除行的代码实际运行之前关闭。我假设您正在使用jQuery进行ajax调用 例如,在html中:Jquery 如何在用户关闭网页时使用.ashx文件执行某些代码,jquery,html,asp.net,vb.net,ashx,Jquery,Html,Asp.net,Vb.net,Ashx,我想在用户关闭网页时执行一些vb.net代码。我该怎么办。我有这个,但我不知道如何使用它。我在某处见过下面的说明,但我不能使用它: 可以将ajax调用绑定到beforeunload页面html事件。只需确保使用async false调用ajax,否则页面将在删除行的代码实际运行之前关闭。我假设您正在使用jQuery进行ajax调用 例如,在html中: <body onbeforeunload="runCode();"> 你的HTML <script type="text/j
<body onbeforeunload="runCode();">
你的HTML
<script type="text/javascript"> function runCode() {
$.ajax({
url: 'YourCode.ashx',
async: false,
success: function(data) {
//Do other things before navigating to other page or closing the browser
}
}); }
</script>
</body>
名为YourCode.ashx的文件将包含删除数据库行的代码,或者您希望执行的任何操作。这样,即使用户关闭浏览器,代码也将运行。请注意,如果YourCode.ashx中的代码有问题,异步假调用可能会锁定页面。因为您使用的是jQuery,所以可以使用。确保包含jQuery
仅供参考:如果使用jQuery卸载事件,请删除onbeforeunload=runCode;从body标签
那个么你们面临的问题是什么?我不熟悉。阿什克斯很好。例如,如何在关闭页面之前使用此方法显示消息?我想在.ashx文件中实现此代码:MsgBoxhi您还可以看到此页面:[有一个错误显示:Microsoft JScript运行时错误:“$”未定义我不使用jquery,那么我应该使用什么?@user3724490$。ajax是。虽然您不知道,但您正在使用jquery,但未包含jquery脚本。这就是为什么您得到Microsoft JScript运行时错误:“$”未定义错误。
<body>
<form id="form1" runat="server">
This is content.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js">
</script>
<script type="text/javascript">
$(window).unload(function () {
$.ajax({
url: 'YourCode.ashx',
async: false,
success: function (data) {
// Do other things before navigating
// to other page or closing the browser
}
});
});
</script>
</form>
</body>