In my program, if a user tries to leave a page, he'll receive a dialog box asking if he is sure he wants to leave.
How should I implement the 'cancel' option if the user chooses not to leave the page?
Source javascript code:
$(window).unload(function(){
var c= confirm ("Are you sure?");
if (c){
alert("Thanks. Good luck!");
}
else{
????
}
});
window.onbeforeunload = function() {
return 'You have unsaved changes!';
}
many question about this in stackoverflow
How can I override the OnBeforeUnload dialog and replace it with my own?
JavaScript + onbeforeunload
jQuery(window).on('beforeunload',function(){
var value = navigateAway(isDirty);
if(value=="changed")`enter code here`
{
if(jQuery("#saveCheck").val()=="false")
{
return "<?php echo JText::_('OVR_WANT_TO_LEAVE');?>";
}
}
});
Related
I am trying to create an alert, to insure that the user is submitting the correct information and if 'Ok' is clicked rather than cancel, the link is clicked and <a> sent. I have nearly achieved it, the alert activates, however does not activate if ok is clicked. Unfortunately, I am not a js wizard, yet..
EDIT :
click => preventDefault => alert (yes, no) if yes(send) if no dont.
<script>
jQuery(document).ready(function($){
$("input.btn-default").click(function(e){
e.preventDefault();
var answer=confirm('Are you sure that you want to do this?');
if(answer == true){
///not sure how to remove the prevent default and send link?!
}
else{
return;
}
});
});
</script>
Try the following code using return false; if the user cancel confirm dialog and window.open( $(this).att('href') ); to open the link when he user click OK :
$("input.btn-default").click(function(e)
{
e.preventDefault();
if( confirm('Are you sure that you want to do this?') )
{
window.open( $(this).att('href') );
}
else
{
return false;
}
});
Hope this helps.
confirm() is modal, so you just need to check for answer:
jQuery(document).ready(function ($) {
$("input.btn-default").click(function (e) {
var answer = confirm('Are you sure that you want to do this?');
if (!answer) {
e.preventDefault();
}
});
});
I have a problem with this script. When I press 'yes' it deletes the user which is fine. My problem is that it still deletes the user when I click cancel. Any ideas?
<script>
function myFunction() {
var x;
if (confirm("Are You Want To Delete This User ?") == true) {
x = "delete.php";
} else {
x = "memberlist.php";
}
document.getElementById("demo").innerHTML = x;
}
</script>
The mentioned JavaScript works fine. It may be that there is some issue in your delete.php or memberlist.php code. For a solution as per your expectations, please provide the code for delete.php and memberlist.php.
I assume your element is a link with "delete.php" as href - that is a VERY bad idea unless you want google spider to delete your database
You need to return false on a link's click event if you do not want it to execute - but see 1
I suggest
window.onload=function() {
document.getElementById("deleteLink").onclick=function() {
if (confirm("Do you really want to delete?") {
location="delete.php?user="+this.getAttribute("data-user");
}
}
return false // cancel link
}
using
Delete
Generic for more than one user:
window.onload=function() {
var deleteLinks = document.querySelectorAll(".deleteLink");
for (var i=0;i<deleteLinks.length;i++) {
deleteLinks[i].onclick=function() {
var user = this.getAttribute("data-user");
if (confirm("Do you really want to delete "+user+"?") {
location="delete.php?user="+user;
}
}
}
return false // cancel link
}
using
Delete Frank
Delete Bob
you can add confirm in your url
Edit : sory about spelling mistake i just want to show an alternative way i am texting on phone
<a onclick="return confirm('you are going to delet it are you sure bobo ?')" href="#your delet url">
Just wondering, is it possible to create an alert with multiple options?
Like for example, in Facebook, when you try to close the tab/window when you have not finished typing your message, an alert with the options "Leave this page" and "Stay on this page" will pop up.
Example with form, you'are looking for window.onbeforeunload:
<script type="text/javascript">
var originalFormContent
var checkForChanges = true;
jQuery(document).ready(function () {
originalFormContent = jQuery('#myForm input[type=text]').serialize() + jQuery('#myForm select').serialize();
});
function onClose() {
if (checkForChanges && originalFormContent != "undefined") {
var content = jQuery('#myForm input[type=text]').serialize() + jQuery('#myForm select').serialize();
if (content != originalFormContent) {
return confirm('You have unsaved changes. Click OK if you wish to continue ,click Cancel to return to your form.');
}
}
}
window.onbeforeunload = onClose();
You're referring to window.onbeforeunload:
Best way to detect when a user leaves a web page?
You can also use the window.confirm() function for OK/Cancel options:
http://jsfiddle.net/UFw4k/1
Other than that, you'd have to implement a custom modal alert, such as jQuery Dialog.
Please have a look at http://jqueryui.com/dialog/#default
Copied this from a previous answer: JavaScript alert with 3 buttons
<a href="Profile.aspx?ProfileId=1" class="view-profile">View profile<a>
When the user click on my href there is an treatment=" .ajax script that will allow or the user to view the profile."
the problem is when that when the user right click my href,the treatment is ignored and the user can see the all profiles.
How can I prevent the right click?
Or how can I execute the same treatment(check) when the user right click my href.
I have tested the below code but not working:
$(function() {
$('.view-profile').bind("contextmenu", function(e) {
return false;
});
});
Thanks in advance.
You shouldn't rely on client-side validation to control user privileges, because it can be easily hacked; rather, use both server-side and client-side validation or simply server-side alone.
<script language=JavaScript>
<!--
//Disable right mouse click Script
//By Maximus (maximus#nsimail.com) w/ mods by DynamicDrive
//For full source code, visit http://www.dynamicdrive.com
var message="Function Disabled!";
///////////////////////////////////
function clickIE4(){
if (event.button==2){
alert(message);
return false;
}
}
function clickNS4(e){
if (document.layers||document.getElementById&&!document.all){
if (e.which==2||e.which==3){
alert(message);
return false;
}
}
}
if (document.layers){
document.captureEvents(Event.MOUSEDOWN);
document.onmousedown=clickNS4;
}
else if (document.all&&!document.getElementById){
document.onmousedown=clickIE4;
}
document.oncontextmenu=new Function("alert(message);return false")
// -->
</script>
--source: http://www.dynamicdrive.com/dynamicindex9/noright.htm
I'm trying to replicate the same thing as on the stackoverflow textareas.
EDIT: I went with something like this, after reading the suggestions here:
window.onbeforeunload = function()
{
var myTextArea = document.getElementById('post_content');
if (myTextArea.value.length > 0)
{
return "You haven\'t submitted your post; are you sure you want to discard it?";
}
}
This seemed to work with Firefox and other browsers without causing double confirmations.
Use something like:
window.onunload = function(){
var myTextArea = [a ref to your textarea];
if (myTextArea.value.length > 0) {
//=>textarea contains text, ask the user:
return confirm('You didn\'t submit your text yet! Are you sure'+
' you want to navigate away from this page?');
}
//=>continue unloading
return true;
}