Call a JavaScript function when user leaves a page

I want the function `pageleave()` to be called before the user exits a page. But I can't seem to get it to work correctly, I searched the web for a couple hours and can't find a solution. Here is the code I came up with. It runs when the page loads instead of when the page unloads. window.onbeforeunload = pageleave; function pageleave() { $.post("/ajax/videocanle.php", { videoID: "" }, function(data) { if ( data != "\nsuccess"){ alert ("Sorry but there was an unexpected error, please try again"); } }); } That is the current revision, it still doesn't run the function before the page unloads. The function does work thou, I tested it with a simple input button.

以上就是Call a JavaScript function when user leaves a page的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准