Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeonion/470a871d4934889d78e2a5b0e3d65380 to your computer and use it in GitHub Desktop.
Save codeonion/470a871d4934889d78e2a5b0e3d65380 to your computer and use it in GitHub Desktop.
Javascript Browser Back Button Control
<!DOCTYPE html>
<html>
<head>
<title>Prompt test</title>
</head>
<body onbeforeunload="HandleBackFunctionality()" onunload="">
Please enter your name<br>
<input type="text" id="field1">
<input type="button" value ="oka" id="field2">
</body>
<script>
window.onbeforeunload = function() {
if(document.getElementById("field1").value.length > 0){
return "You work will be lost.";
}
};
function HandleBackFunctionality()
{
if(window.event)
{
if(window.event.clientX < 40 && window.event.clientY < 0)
{
alert("Browser back button is clicked...");
}
else
{
alert("Browser refresh button is clicked...");
}
}
else
{
if(event.currentTarget.performance.navigation.type == 1)
{
alert("Browser refresh button is clicked...");
}
if(event.currentTarget.performance.navigation.type == 2)
{
alert("Browser back button is clicked...");
}
}
}
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment