"Leaving website" alert - javascript

I have the following href on my site:
a href="http://www.google.com" name="external" onclick="confirmExit(this.name)
I just can't figure out how to make user stay on my website if he/she doesn't want to leave by clicking "cancel" (incomplete code below).
function confirmExit(name){
if(name == "external"){
if(confirm("Go to external site?")){
}
}
}
jQuery/Ajax is not an option.

Just return false.
function confirmExit(name){
if(name == "external"){
if(!confirm("Go to external site?")){
return false;
}
}
}
If you want this to happen on all links, or even when the user closes the tab, check out #megawac's answer.

Use window.onbeforeunload to show a confirm dialog before a user leaves the page
$(window).bind("beforeunload", function() {
if(true) { //display confirm dialog?
return "Are you sure you want to leave?";
}
});

You don't need a function. I suggest you to use this:
$(function () {
$('a[name="external"]').click(function () {
if (!confirm("Go to external site?")) {
event.preventDefault();
}
});
});
If you use this, you don't need to add onclick="confirmExit(this.name)" to everything. Just add the above and your work will be done.
You can do this if you want behaviour of JSFiddle:
window.onbeforeunload = function(){ return "Are you sure you want to leave the page?"; }

Use the return false inside the confirmExit() function and also use return at inline javascript like
HTML
Some Link
//Use return here also ---^
JS
function confirmExit(name){
if(name == "external"){
if(!confirm("Go to external site?")){
return false;
}
}
}
DEMO

Related

How to fire onClick function of a div depending on another div

I want to call a function on one type of button click on my HTML page.
I have got around 10 such button on the page and wrote the below code to call a function when the button is clicked,
$('.divname').each(function (index) {
$(this).on("click", function (e) {
//Call Another Function
});
});
And this is working fine as expected.
However the issue is, sometimes depending on some condition one dynamically generated div is been created(Pop up message) and I dont want my above said code to work when the pop up message comes up.
Could you please advise how this can be achieved.
Thanks and Regards,
Aniket
Hi What something like set a bool and check if it true or false...somthing like:
var enable = true;
$('.divname').each(function (index) {
$(this).on("click", function (e) {
//Call Another Function
if(!enable) return;
});
});
//set it to false on popup show (or something else event)
$(popup).show(function(){
enable = false;
})
In simplest way.
$('.divname').each(function (index) {
$(this).on("click", function (e) {
//Simply check the condition for that Open Pop up message
if(openPopUp){
return;
}
callfunction();
});
});
try something,
$('.divname').each(function (index) {
$(this).on("click", function (e) {
if($('#popupModal:visible').length == 0){ // check length of visible modals
//Call Another Function
}
});
});

Javascript delete confirmation box in codeigniter

first of all, i already trying maybe all of the possible answer in this site but nothing is working for me.
i want to show pop up windows confirmation before delete my data. i use this
Delete
Maybe you need my controller
public function delete($id = 0) {
$id OR redirect(site_url('admin/barang'));
$this->barang_m->delete($id);
redirect(site_url('admin/barang'));
}
My model barang_m
public function __construct(){
parent::__construct();
parent::set_table('barang','idBarang');
}
public function delete($id = 0) {
if($data = parent::get($id)) {
parent::delete($id);
return true;
}
return false;
}
but when i click cancel it still delete the data even when i clicked 'x' to close the confirmation window. i'm so frustated please help me. Or maybe you can tell me why this problem occurs ?
**i alrady tried this possible answer Codeigniter when i click delete i want pop up notification if if click yes or not
jQuery delete confirmation box
Confirm box in CodeIgniter anchor link to delete record
and many more
try
Delete
and your function like this
function isconfirm(url_val){
alert(url_val);
if(confirm('Are you sure ?') == false)
{
return false;
}
else
{
location.href=url_val;
}
}
You could also try
Delete
And js would be
$('.confirmClick').click(()=> {
var sure = confirm('Are you sure ?');
if(sure){
return true;
}
return false;
})
You need to stop page from opening link when you press cancel.
But as seen in code its looks like you missed condition to check so.
Try this may solved you problem.
Delete
<script>
function isconfirm(){
if(!confirm('Are you sure ?')){
event.preventDefault();
return;
}
return true;
}
</script>
It's because you are running it out of the button itself and returning it. So you're saying, delete the button and return the confirmation.
Probably deleting the return will work, or make a function and make sure it doesn't return it but puts what you want in a variable or something.
<a href="javascript:;" onclick="return confirm_Action('<?php echo base_url(); ?>index.php/admin/delete_department/<?php echo $dept['Dept_id']; ?>');" ><button> DELETE</button></a>
Then, Your function confirm_Action could be like this:
//THis Allows Me To Control The Delete Actions
function confirm_Action(url){
// alert(url);
if(confirm('Are You Sure, This Action Can\'t Be Reversed?') == false)
{
return false;
}
else
{
location.href=url;
}
}
just like how #M. Deepak did it...
Thanks... Hope this helps

Run function after inline confirmation

I wan to run all jQuery codes after inline confirmation, Is this possible?
something like this:
<a href="" onclick="return confirm('are you sure ?')" > test link </a>
I want this code run after inline confirmation above:
$('body').on('click','a',function() {
alert('clicked!');
});
It's important to me that confirmation take place inline, not in jQuery on click.
here is the code which will work for you.
test link
and jquery code is below which will help you.
$('body').on('click','a',function() {
var msg = confirm('are you sure ?');
if (msg){
alert('ok is clicked');
}else{
alert("cancel is click")
}
});
You can do something like this,
function onConfirm()
{
if (!confirm('are you sure ?'))
alert('clicked cancel');
else
alert('clicked confirmed');
};
<a href="#" onclick='onConfirm();'> test link </a>
How about this:
test link
function confirmMessage(msg) {
if (confirm(msg)) {
alert('confirmed');
} else {
alert('failed');
}
}
or
test link
function whenConfirm(is_confirmed) {
var d = $.Deferred();
is_confirmed ? d.resolve() : d.reject();
return d.promise();
}
function confirmed() {
alert('confirmed');
}
function canceled() {
alert('canceled');
}
Try to avoid using inline click handlers. I would also set a class or id on the anchor you intend to put this handling on. Otherwise, you would be binding this confirm on each anchor element in the document.
Confirm
$(document).on('click.confirm','a.confirm-me',function() {
if (confirm('Are you sure ?')){
alert('ok is clicked');
// Returns true;
}else{
alert("cancel is click");
// Returns false;
}
});

Javascript confirm box, if cancel is clicked, it will not reload the page

Is there away that the confirm box appeared, if i clicked "ok" it will go to another page and if i clicked "cancel" it will just stay and the current page will not reload again? THANK YOU.
function reload()
{
var r=confirm("Do you want to leave page!");
if (r)
{
//write redirection code
window.location = "http://www.yoururl.com";
}
else
{
//do nothing
}
}
call this function when you want to confirmation from user.........
You can use confirm() for this, which returns true on ok or false on cancel.
function myFunction(){
if(confirm("Would you like go to other page?")){
window.location = "http://yahoo.com";
}else{
alert('fine, if not want');
}
}
myFunction();
Updated
DEMO
UPDATED2
<button onclick="return logout()" >logout</button>
<script>
function logout(){
if(confirm("Would you like go to other page?")){
window.location = "failed.php";
}else{
//do your stuff on if press cancel
}
}
</script>
You may try doing
<script>
function myfunction(){
if(confirm("The confirm message")){
youDoTheThingsHere();
return false;
}else{
console.log("I cancelled the dialog!");
return false;
}
}
</script>
I'm not sure about your certain situation and the code that is involved when you call the confirm, but this has worked for me. The other option you may look at as a last resort is using something like a bootstrap modal to trigger a "confirm" modal. With the bootstrap modal you can then style it how you want... hope I could help!
Use "return None"
function myFunction(){
if (confirm("Confirm to reset your content!")){
location.reload();
}else{
return None;
}
}

Why do I see confirmation box twice when I write Javascript only once in my document

$("a.sure").click(function () {
var choice = confirm("Are you Sure");
});
I see confirm twice when I write this and click. When I track from Firebug then the confirm dialogue box only appears once.
are you sure you have one only link with .sure class? (it seems a words-trick). Can you provide your markup code? have you tried with
$("a.sure").click(function () {
return confirm("Are you Sure");
});
Triggers only once for me.
Sample HTML:
Sure?
jQuery:
$("a.sure").click(function () {
var choice = confirm("Are you Sure");
return false;
});

Categories

Resources