detect which button is clicked in confirming page navigation [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Capturing result of window.onbeforeunload confirmation dialog
I want to make user confirm page navigation like that:
window.onbeforeunload = function(e) {
return "Note that any unsaved data will be lost";
};
however, I want to detect if user clicked "Stay in this page" to do something important.
So is there anyway to detect which button is clicked?
NB: I tried adding confirm in the onbeforeunload function but chrome block it.

There is no way to execute code right after the user decided to stay on the page.
You can use the onunload event to do something in case the user does not stay. If you want to send an AJAX request that's one of the few cases where async: false is appropriate.

Try like this
$(document).ready(function(){
$('body').live('click',function(){
$clicked = (this).attr("value");
alert($clicked);
});
})
Consider that your buttons value should be your message like:"Note that any unsaved data will be lost"

Related

How to stop refresh the page form the beforeunload event?

i'm using the 'beforeunload' event to detect the refresh event from the webpage.how to stop refresh the page from beforeunload event and i should not show alert message
window.addEventListener("beforeunload", this.onUnload);
onUnload = e => {
e.preventDefault();
// how to stop refresh the page from here and i should not show alert message.
//it is showing alert message. i no need to show the alert.
e.returnValue = "sure do you want to reload?";
}
It is not possible to prevent unloading a page without notifying the user.
Imagine you want to go to github.com at a time you are viewing stackoverflow.com - but it will simply prevent you from navigating away!
However, there was a time some browsers used to prevent unloading silently when you assign an empty string to the returnValue. But I believe that age of evil is gone now.
When i understand your following comment corectly, then you simply need to return zero, or false. So in your function you write:
onUnload = e => {
//e.preventDefault();
return false;
}
When i do this, i get an automated response from the Browser (for me its Chrome), if i really wanna close this website. And its in my language, so its i18n-compilant
It basically says "Are you sure you wanna refresh/close this website, data may get lost" and than 2 buttons with "Refresh/Close" and "Abort"

Confirm redirection from page in javascript

i am working on asp.net site that contains say three pages
page1.aspx
page2.aspx and page3.aspx
I want if user redirects from page1, alert box asking for confirmation should come. For this i have written following code
window.onbeforeunload = function () {
return 'Are you sure you want to leave?';
};
But this code run on every postback to server including pressing F5.
I want this code to run only when user redirects to any of remaining two pages
how can do this ??
try this code in javascript onload event in the pages only
if(confirm("Are you sure you want to leave?"))
{
//redirect to next page
return false
}
Handling in window.onbeforeunload may not be the straight forward way to do it since the alert message will be shown each and every time you load the page.
What you could do instead is call the confirmation js function in the from the DOM object that could possibly take the user to the different page.
For instance, let us suppose the below anchor tag takes the user to page two:
<a id="pageTwoAnchorTag" runat="server" href="~/Page2.aspx">Page 2</a>
you could add the below onclick event to get your confirmation when the user wants to go the page 2:
<a id="pageTwoAnchorTag" runat="server" onclick="return confirm('Are you sure you want to leave?')" href="~/Page2.aspx">Leave Records</a>
Only asking for confirmation in a couple of places might not be fully satisfying, since it will not trigger in all desired cases. For example, when you close the browser tab or window, your data will be lost.
I suggest to do the opposite: Register for the onbeforeunload as you already do, but include a "guard" which you disable on any action that should not trigger the confirmation (i.e. in postbacks):
window.shouldconfirm = true;
window.onbeforeunload = function () {
if (shouldconfirm) {
return 'Are you sure you want to leave?';
}
};
Register for the events that occur before a postback is executed (e.g. button click events, etc.) In those places, disable the confirmation:
window.shouldconfirm = false;
I am slightly surprised that the postbacks trigger the onbeforeunload event. I though to remember that on form posting the event is not triggered, and that a ASP.NET postback is implemented as a form post action.

How to disable the back button in the browser using JavaScript [duplicate]

This question already has answers here:
How can I stop the browser back button using JavaScript?
(38 answers)
Closed 8 years ago.
Locked. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.
I want to disable the back button for a website.
Whenever the person clicks on the browser back button it should not be able to go on the page the user visited before.
history.pushState(null, null, document.title);
window.addEventListener('popstate', function () {
history.pushState(null, null, document.title);
});
This script will overwrite attempts to navigate back and forth with the state of the current page.
Update:
Some users have reported better success with using document.URL instead of document.title:
history.pushState(null, null, document.URL);
window.addEventListener('popstate', function () {
history.pushState(null, null, document.URL);
});
One cannot disable the browser back button functionality. The only thing that can be done is prevent them.
The below JavaScript code needs to be placed in the head section of the page where you don’t want the user to revisit using the back button:
<script>
function preventBack() {
window.history.forward();
}
setTimeout("preventBack()", 0);
window.onunload = function() {
null
};
</script>
Suppose there are two pages Page1.php and Page2.php and Page1.php redirects to Page2.php.
Hence to prevent user from visiting Page1.php using the back button you will need to place the above script in the head section of Page1.php.
For more information: Reference
Our approach is simple, but it works! :)
When a user clicks our LogOut button, we simply open the login page (or any page) and close the page we are on...simulating opening in new browser window without any history to go back to.
<input id="btnLogout" onclick="logOut()" class="btn btn-sm btn-warning" value="Logout" type="button"/>
<script>
function logOut() {
window.close = function () {
window.open('Default.aspx', '_blank');
};
}
</script>
You can't, and you shouldn't.
Every other approach / alternative will only cause really bad user engagement.
That's my opinion.

onclick submit button JSP [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Multiple submit Button click problem?
Have a type=Submit button in a JSP. Requirement is to disable this button on click.
The basic requirement is to stop posting multiple requests of the same form if this button is clicked multiple times for the same form.
Thank You.
Even if you disable the submit button the user can still submit the form by pressing F5 or ctrl+F5 or even using Browser reload button.So disabling the submit button won't be of much use.
Why not use Post/Redirect/Get method.
The PRG pattern basically redirects the user to another page when it is Posted.
Hence now when the user reloads or tries to resubmit the form he is calling the Get method so the form is prevented from resubmission.
However this is also not fully secured approach.
Look here: http://www.developertutorials.com/tutorials/javascript/how-disable-submit-button-050416-1275/
I use a simple JS function you can invoke with onlick():
<script type="text/javascript">
var isSubmitted = false;
function safeSubmit(entityName) {
if (!isSubmitted) {
isSubmitted = true;
document.forms[0].submit;
}
}
</script>

Can i detect/capture the Broswer Back button using javascript? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is there a way to catch the back button event in javascript?
In my application,the user can enter data eithe add or modify those modification is saved into database using ajax.After all the modifications is completed in the screen then user will be finalized the data.In the time,user accediently click on Browser 'Back' Button.
So,what i need is,if user clicked without 'Update' button then if clicks the Browser 'Back' button.i want to shown the alert message.So,How to detect/capture the 'Back' Button.Please help me
You could use the window.onbeforeunload event
<body onunload="Unloader()">
<script type="text/javascript">
function Unloader() {
}
</script>

Categories

Resources