We hope you enjoy your visit.

You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.


Join our community!


If you're already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
Ask Users to Confirm Before Leaving a Page with Post Data; Prevents the loss of posts due to careless link clicking or accidental refreshes.
Topic Started: Jun 16 2012, 07:42 PM (2,782 Views)
Makaze
Member Avatar
Discordian
[ * ]
Code Name:

Ask Users to Confirm Before Leaving a Page with Post Data

What the code does:

When a user tries to leave a page or reload the same page, this script will check to see if they have entered post data. If they have, it gives them a prompt warning them that they have unsaved data and asks them if they really want to leave.

Currently applies to:

  • Posts
  • PMs
  • PM Quick Compose
  • Fast Reply
How to use the code:

The script is passive. No user input is required.

How to install the code:

Enter the following code under Admin CP » Board Template » Below the Board:

Code:
 
<script type="text/javascript">
// Ask Users to Confirm Before Leaving a Page with Post Data
// by Makaze of Zathyus Networks
window.onbeforeunload = confirmExit;
function confirmExit() {
var postField = $("#c_post-text").length;
var fastReplyField = $("#fast-reply dd textarea").length;
var pmField = $("#quickcompose").length;
var confirmMsg = "You have unsaved data. If you continue, your data may be lost."; // Customize the prompt message. The text "Are you sure you want to leave this page?" is automatically appended on the next line.

if (postField != 0) {
var emptyPost = $("#c_post-text").val();
if (emptyPost != "") {
return confirmMsg;
};
};

if (fastReplyField != 0) {
var emptyFastReply = $("#fast-reply dd textarea").val();
if (emptyFastReply != "") {
return confirmMsg;
};
};

if (pmField != 0) {
var emptyPM = $("#quickcompose").val();
if (emptyPM != "") {
return confirmMsg;
};
};
};

function justExit() {

};

$('#c_postbtn button, #fast-reply button, .postbtn button:contains("Send"), #pm_compose td:contains("Send Message") button').click(function() {
window.onbeforeunload = justExit;
});
</script>

Extra notes:

My spacing is entirely for readability, and an experienced editor may remove empty spaces and newlines to save space if necessary. Credits are also unnecessary, but if you could leave it, I would appreciate it.
Edited by Makaze, Jun 16 2012, 07:53 PM.
Offline Profile Quote Post Goto Top
 
Geoffrey
Member Avatar
bury hatchets, keep maps
[ *  *  *  *  * ]
Ah nice, this is a pretty useful code. :)
Offline Profile Quote Post Goto Top
 
SandyC (PM)
Member Avatar
Member
[ *  *  *  * ]
Good idea, I need something like this, lol...
Offline Profile Quote Post Goto Top
 
Dannyy
Member Avatar
Member
[ *  * ]
Wow, every single forum needs something like this. I've lost my data on numerous occasions when I've typed directly into my homework submissions at school as well.

This code is so useful. Thank you!!
Offline Profile Quote Post Goto Top
 
NielsGuchelaar
Member
[ * ]
Amazing code ! Thank you :)
Offline Profile Quote Post Goto Top
 
2wise69
Member Avatar
Member
[ *  * ]
Nice. Thank you for this.
Offline Profile Quote Post Goto Top
 
Dream Endlessly
Member
[ * ]
Appreciate it. Using!
Offline Profile Quote Post Goto Top
 
ringthebellamy
Member Avatar
Dum Spiro Spero
[ * ]
I have experienced problem with this javascript. Only two times so far in my whole experience, but it needs me to restart my iMac in hard way (turn it off by turn off the electricity). I'd appreciate it if someone can tell me whether there's something wrong with the script, the browser, or the computer.

So I just casually type something on the board, then I accidentally swiped my mouse so it goes to last or next page. Obviously, the script would ask me and give me option to "stay on page" or "leave page", right? In most times, I can click one of those two options and everything is fine. But sometimes, it seems to be broken in a way that I clicked on "stay on page" or "leave page" and nothing happens. It's just there, keep asking me the same question again and again.

And I haven't find any way to stop it from doing this except take off the electricity from the computer; it doesn't want to close Mozilla Firefox, the browser I use. Which means, my iMac won't restart the normal way (the usual way, you command it via Apple Logo > Restart...) because the Firefox is still on and needs to be closed before restarting.

But I really like this script and it has been working nice big time, only sometimes, when it breaks... let's say it breaks hard.

I'm taking this script off from my board for a while until I can understand this weird behavior of the script (or its relation with my browser and or my computer).
Offline Profile Quote Post Goto Top
 
Twinkle And Kessie
Member Avatar
Also Known As Carol And Nella
[ *  * ]
I hope the incident that happened to ringthebellamy has been resolved since this code was posted, I really need this code for my own board but I don't want issues like these happening to my members. /-:
Offline Profile Quote Post Goto Top
 
JadedOne-ZNS
Member Avatar
Member
[ *  *  * ]
Twinkle And Kessie
Aug 28 2013, 04:08 AM
I hope the incident that happened to ringthebellamy has been resolved since this code was posted, I really need this code for my own board but I don't want issues like these happening to my members. /-:
If you have trouble with this one you could always make a request for an updated one in the requests section.
Offline Profile Quote Post Goto Top
 
DarkTamerx
Member Avatar
Member
[ * ]
I personally have used this for a while and haven't had any problems.
Offline Profile Quote Post Goto Top
 
Makaze
Member Avatar
Discordian
[ * ]
ringthebellamy
Nov 14 2012, 09:43 PM
I have experienced problem with this javascript. Only two times so far in my whole experience, but it needs me to restart my iMac in hard way (turn it off by turn off the electricity). I'd appreciate it if someone can tell me whether there's something wrong with the script, the browser, or the computer.

So I just casually type something on the board, then I accidentally swiped my mouse so it goes to last or next page. Obviously, the script would ask me and give me option to "stay on page" or "leave page", right? In most times, I can click one of those two options and everything is fine. But sometimes, it seems to be broken in a way that I clicked on "stay on page" or "leave page" and nothing happens. It's just there, keep asking me the same question again and again.

And I haven't find any way to stop it from doing this except take off the electricity from the computer; it doesn't want to close Mozilla Firefox, the browser I use. Which means, my iMac won't restart the normal way (the usual way, you command it via Apple Logo > Restart...) because the Firefox is still on and needs to be closed before restarting.

But I really like this script and it has been working nice big time, only sometimes, when it breaks... let's say it breaks hard.

I'm taking this script off from my board for a while until I can understand this weird behavior of the script (or its relation with my browser and or my computer).


Twinkle And Kessie
Aug 28 2013, 04:08 AM
I hope the incident that happened to ringthebellamy has been resolved since this code was posted, I really need this code for my own board but I don't want issues like these happening to my members. /-:


This sounds like a problem with the browser. There are no loops in my code and the event it uses is a built in feature of the browser (it is bound to the "on before unload" event). An alternative: it is possible that another script that does have loops is causing a conflict?
Offline Profile Quote Post Goto Top
 
Helena
Member Avatar
M is for Mod

For those that might not be aware, there are browser extensions available that will back up your textarea inputs for you. Lazarus is a good one in FireFox, for instance. Its saved me a lot of grief.
Offline Profile Quote Post Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · ZetaBoards Codes & Modifications · Next Topic »
Add Reply