browsertabが閉じたときに関数を開く必要があります(これもalt + f4)。そのためのJavaScript機能のようなものはありますか?
2 に答える
3
一般的なケースでは、 onbeforeunloadを使用してのみ標準の確認ダイアログを表示できます。
(明らかなセキュリティ上の理由から)任意のjavascript関数を実行することはできません。
例外は、このタブがまだ開いていて同じドメインにある別のタブから開かれた場合です。この場合、子がまだ開いているかどうかをオープナーから確認できます(ただし、閉じる前にコードを実行することはできません)。
あなたはこのようにそれをテストするかもしれません:
var tab = window.open("index2.html");
setInterval(function(){console.log(tab.window)}, 2000); // shows that tab.window is null as soon as you close the child tab
編集:実際には、onbeforeunloadコールバックを使用していくつかのことを行うことができますが、信頼性が低く、すべてのブラウザーで機能するわけではなく、アラートを許可したり、ウィンドウの閉じをブロックしたりすることはできません。
于 2012-09-12T14:58:36.880 に答える
-1
jQueryのアンロード機能を利用できます
<script type="text/javascript">
$(document).ready(function(){
$(window).unload(function(){
alert("Close this window to show Me Unload Event occur..!");
});
});
</script>
これを試して
<!DOCTYPE html>
<html>
<head>
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
$(window).unload(function(){
alert("Goodbye!");
});
});
</script>
</head>
<body>
<p>When you click <a href="http://www.google.com">this link</a>, or close the window, an alert box will be triggered.</p>
</body>
</html>
私のFirefoxで動作しています
于 2012-09-12T15:01:50.337 に答える