how can I check page edited and confim leave page - javascript

I want implement a method like facebook , when there is edited data in page confirm that he/she want exit page without save them,
I found below code to get confirm,
window.onbeforeunload = function(){
return "Are you sure you wanna leave without saving edited data?";
}
I know I should write a code that check data change by client side (js(jquery)), my first idea is check input element changes something like below code
var edited=false;
$('input ,select').change(function(){
edited=true;
});
window.onbeforeunload = function(){
if(edited)
return "Are you sure you wanna leave without saving edited data?";
}
what is the full method of checking edit occur in page and confirm user to leave if there was change in page.

my idea was correct and work correctly,
I write it here to help others in future
(function($){
var edited=false;
$('input ,select').change(function(){
edited=true;
});
window.onbeforeunload = function(){
if(edited)
return "Are you sure you wanna leave without saving edited data?";
}
})(jQuery);

you could implement the check directly on the controller, for example:
<input type="text" name="name" onChange="var edited = true"> </input>
then, when this code's going to be executed:
window.onbeforeunload = function(){
if(edited)
return "Are you sure you wanna leave without saving edited data?";
}
if the inputbox has changed, it will find edited variable set to true and it will ask to the user if he/she's sure to leave the page, without saving the data.

Related

How would I save input from a text box and then move to the next page? (Javascript/HTML)

As part of my new job, I'm creating a small form where users answer a question and this is then saved and output at the end of the pages.
I started off with having a prompt where users were asked to explain their answers (which worked perfectly!), however I've been asked to change this to an input box.
Essentially the process I need to do is:
User enters in text box -> Clicks next button -> save input to session variable and move to next page
So far, I have the following HTML in the body:
<form name="next" action='#' method=post>
Explanation:<input type="text" id="xp" required><br>
<button class="nextButton" onclick="return explanation()">Next</button>
</form>
with the corresponding javascript:
function explanation() {
var exp = document.getElementById('xp').value;
sessionStorage.setItem("p1_reason", exp);
alert(exp);
document.location.href = 'page2.html';
}
So far the result of this is:
The text box is cleared, but nothing is saved or displayed onscreen
The next page is not displayed.
Any help/advice would be appreciated. I'm relatively new to js so I'd be grateful! I'm well aware that there are similar questions around, I just can't seem to see where I'm going wrong.
#David is right. You can add event.preventDefault() function to prevent the form from its default behaviour, which is submitting. Otherwise your code seems to work.
function explanation() {
event.preventDefault(); // <-- add here
var exp = document.getElementById('xp').value;
sessionStorage.setItem("p1_reason", exp);
alert(exp);
window.location.href = 'page2.html';
}
Also, don't use document.location.href, it's deprecated. It's better to use window.location.href instead.
When you click on nextButton, the browser run explanation() and then try to execute the action of your form. Because your action is action='#' it just try to reload the page, preventing document.location.href for working properly.
Actually, you can try to don't enter nothing on the box and click on the button. The redirect will work because the form is empty, so there is nothing to submit.

How can I prevent the JavaScript Prompt Leaving site From Appearing

Hey guys I am trying my best to figure it out how to remove the Javascript prompt/confirm that says "Do you want to leave this site?" like this:
https://prnt.sc/famast
Basically what's happening here is that when a modal got opened and the user click on "YES" it will redirect to a page. But I don't want the JavaScript confirmation but just redirect it to that page.
Any idea if you know some scripts that could make it happen?
Please help!
As other said above me, you can do it with onbeforeunload:
window.onbeforeunload = function() {
return '';
// The browser shows a pre-defined message so you don't have to write your own
}
You can also use addEventListener, in this way:
window.addEventListener('beforeunload', function() {
return '';
});
See an example (Link updated)

How to capture when a user is leaving ASP.Net page unexpectedly

I need to prompt a user when they are leaving my ASP.Net page unexpectedly with a message to ask if they are sure they want to leave. A post back or when the save button is clicked should not fire the warning. There are a bunch of articles covering this but I am brand new to this and appear to have got my wires crossed.
The recommended way appears to be to use the window.onbeforeunload event but behaves unexpectedly for me. This is fired when the page loads as opposed to when the page unloads.
<script language="JavaScript" type="text/javascript">
window.onbeforeunload = confirmExit;
function confirmExit() {
return "You have attempted to leave this page. If you have made any changes to the fields without clicking the Save button, your changes will be lost. Are you sure you want to exit this page?";
}
</script>
If I use the JQuery implementation it fires when the page unloads but the problem is it fires before the code behind is executed. So I cannot set a variable on the client saying don’t fire the event this time as it is a post back or a Save.
$(window).bind('beforeunload', function () {
return 'Are you sure you want to leave?';
});
Can anyone point me in the correct direction as I know I am making basic mistakes/miss-understanding?
Edit:
So I am nearly there:
var prompt = true;
$('a').live('click', function () {
//if click does not require a prompt set to false
prompt = false;
});
$(window).bind("beforeunload", function () {
if (prompt) {
//reset our prompt variable
prompt = false;
//prompt
return true;
}
})
Except the problem is in the above code I need to be able to differentiate between the clicks but I haven't been able to figure that out yet i.e. I am missing a condition here "//if click does not require a prompt set to false".
Any ideas?
Thanks,
Michael
You can try using this:
$(window).unload(function(){
alert('Message');
});
In case people are interested this is the roundabout solution to my problem. How to tell if a page unload in ASP is a PostBack

tinymce dont show the alert leaving the page

I am using jquery with normal version 3.4.3.2 (non-jquery).
I am using jquery to get contents of the box like this
$(function(){
$(".submit").click(function() {
var text = tinyMCE.get('elm1').getContent();
$(".here").html(text);
return false;
});
});
But when i edit the content of the textarea and click submit and then leave the page.
But it shows the alert box.
I dont want it to come if the users have clicked the .submit after editing.
Thanks
Brett seems right.
You can even use this to get the content into the form in case $(."here") ist the element you initialized the tinymce editor for:
$(".submit").click(function() {
tinymce.get('elm1').save();
return false;
});
Sorry to post in an old topic but I tried .save() and it did not work for me either. So, I figured out a better solution that I wanted to share for the next person.
The unsaved alert is part of the autosave plugin which checks function tinymce.get('mce1').isDirty(). This function checks the value of tinymce.get('mce1').isNotDirty
A "dirty" mce is one which has unsaved changes. So, to prevent the popup you have to tell mce that it is clean without any unsaved changes.
tinymce.get('mce1').isNotDirty = 1;//clean mce
That will prevent the popup when leaving/refreshing page, assuming no more edits are done to the content of mce1
If you have one editor per page try
tinymce.activeEditor.save();
See the documentation for more details
https://www.tiny.cloud/docs/api/tinymce/tinymce.editor/

Yes/No box in Javascript like this one here in StackOverflow?

I want to know how can I display a confirmation box when the users try to get out of the page, like this one here in StackOverflow when you try to close the "Ask Question" page.
Thanks you in advance.
Actually, all that is necessary is this:
window.onbeforeunload = function(){ return "You should save your stuff."; }
This is also kind of a dupe of this: How to show the "Are you sure you want to navigate away from this page?" when changes committed?
In javascript, attach a function to window.onbeforeunload, like so:
window.unbeforeunload = function (e)
{
// return (prompt ('Are you sure you want to exit?'));
// EDIT: Amended version below:
var e = e || window.event;
if (e) e.returnValue = 'Your exit prompt';
return 'Your exit prompt';
}
EDIT:
As the comments below indicate, I had misunderstood the working of the event. It should now work as intended.
Ref: window.onbeforeunload (MDC)
probably a dupe: How do you prevent a webpage from navigating away in JavaScript?, but you can do this by adding an delegate to the window.onbeforeunload event
<script type="text/javascript">
window.onbeforeunload = function() {
//will show a dialog asking the user if
//they want to navigate away from the current page
// ok will cause the navigation to continue,
// cancel will cause the user to remain on the current page
return "Use this text to direct the user to take action.";
}
</script>
Update: doh! updated code to do what the OP really wanted :D
You want to catch the onbeforeunload event of window. Then if you return a string, the browser will display your message in a prompt. Here's an article with sample code.
You can use the window.onbeforeunload event to catch this. If there is any value inside the textarea then an alert message is shown.
That's browser based.
As long as you implement <form> tag, and you type in something in the form, and in Google Chrome, it will prompt you that message.

Categories

Resources