JavaScript pop box won't work properly - javascript

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">

Related

Calling first button on confirm of second button in javascript isn't working

I have two buttons in my html page and when someone clicks Generate button I want to save my document (the same thing save button is doing) if ok is clicked otherwise I do not want to do anything.
Js code
$("#save-application").trigger("click");
I also tried to use
document.getElementById('save-application').click();
and
$('#save-application').click();
But it isnt working.So basically I want to call my save button.Any suggestions on this?
I did tried
if (check == true) {
$("#form").submit();
return true;
}
else {
return false;
}
but form doesn't get saved
I think you missed # while querying by id $("#save-application").click();.
Instead of using trigger , you can create separate function for save functionality,
html code :
<input type="submit" value="Save" id="save-application" onclick="saveApplication();"/>
js code :
function saveApplication(){
//logic here
}
and then call saveApplication() instead of $("save-application").trigger("click");
So I'd do something like this on your input
<input id="save-application" type="submit" value="Save" onclick="saveApplication()"/>
Then modify your javascript to something like this:
function saveApplication() {
if (isChanged == true ) {
var check = confirm("Would you like to to save your changes and have a document generated?");
if (check == true) {
$("#save-application").trigger("click");
return true;
}
else { return false; }
} else {
return undefined;
}
}
Hope this helps

jquery preventDefault on alert, if alert = ok, then send

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();
}
});
});

How can I prevent a page unload with jQuery?

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');?>";
}
}
});

How redirect cancel function in Javascript

I'm not a programmer. I don't want to protect with a strong secure code my page. I just need one option I'm missing in my code and can't figure out how to add it.
<script language="Javascript">
<!--
var password = "lala"
var x = prompt("","")
if (x.toLowerCase() == password) {
location = "http://google.com";
}
else {
alert("Fail")
location = "http://facebook.com"
}
//-->
</script>
As you can see it's so dump but I need it. When I press Cancel button instead of writing true or false text, website still opens. I want to include in this script cancel button function (control it, you know) whitch would redirect to another website if press on it (as it is with true or false functions). I don't want to creat a special button or an input for it.
Update: I would like to include this script in a page which i am redirecting to. Could anyone tell me:
1. How can i modify this script to make it work only once?
2. Is it anything to do with browser's cookies?
p.s. Done :)
If the user presses cancel, prompt will return null. So do like this:
if(x == null) // Cancel
{
alert('Cancel');
}
else if (x.toLowerCase() == password) { // Correct password
location = "http://google.com";
}
else { // Wrong password
alert("Fail")
location = "http://facebook.com"
}
However, I'm not sure if all browsers will return null when the user presses cancel. (I have tested in Opera)
Try
var x = prompt("","");
if( x == null || x == '')
return;
if (x.toLowerCase() == password) {
location = "http://google.com";
}
else {
alert("Fail")
location = "http://facebook.com"
}
To redirect your browser to a URL use following snippet in your Javascript:
top.location.href = "http://google.com";

How do I warn a user before closing a page when textarea is not empty?

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;
}

Categories