PreventDefault SweetAlert2












1















I got this function that override my native js alerts:



function alert(message, title = 'Test', type = 'info')
{
// event.preventDefault();
if(typeof(swal) != 'undefined') {
swal({
html: message,
title: title,
type: type,
width: '24rem',
}).then((result) => {
return result.value;
});
}
else {
alert(message);
}
}


At the end of my PHP functions, i have an alert('success'), and then i redirect to another page. With the native JS alert, it waits me to click the OK button to continue. Now with this swal function, it shows the alert and redirects immediatly. Is there a way to avoid this behavior and act like the native alert, without changing the function signature?










share|improve this question























  • it is a js window.location.href

    – Ademilson Santana da Silva
    Nov 13 '18 at 17:06
















1















I got this function that override my native js alerts:



function alert(message, title = 'Test', type = 'info')
{
// event.preventDefault();
if(typeof(swal) != 'undefined') {
swal({
html: message,
title: title,
type: type,
width: '24rem',
}).then((result) => {
return result.value;
});
}
else {
alert(message);
}
}


At the end of my PHP functions, i have an alert('success'), and then i redirect to another page. With the native JS alert, it waits me to click the OK button to continue. Now with this swal function, it shows the alert and redirects immediatly. Is there a way to avoid this behavior and act like the native alert, without changing the function signature?










share|improve this question























  • it is a js window.location.href

    – Ademilson Santana da Silva
    Nov 13 '18 at 17:06














1












1








1








I got this function that override my native js alerts:



function alert(message, title = 'Test', type = 'info')
{
// event.preventDefault();
if(typeof(swal) != 'undefined') {
swal({
html: message,
title: title,
type: type,
width: '24rem',
}).then((result) => {
return result.value;
});
}
else {
alert(message);
}
}


At the end of my PHP functions, i have an alert('success'), and then i redirect to another page. With the native JS alert, it waits me to click the OK button to continue. Now with this swal function, it shows the alert and redirects immediatly. Is there a way to avoid this behavior and act like the native alert, without changing the function signature?










share|improve this question














I got this function that override my native js alerts:



function alert(message, title = 'Test', type = 'info')
{
// event.preventDefault();
if(typeof(swal) != 'undefined') {
swal({
html: message,
title: title,
type: type,
width: '24rem',
}).then((result) => {
return result.value;
});
}
else {
alert(message);
}
}


At the end of my PHP functions, i have an alert('success'), and then i redirect to another page. With the native JS alert, it waits me to click the OK button to continue. Now with this swal function, it shows the alert and redirects immediatly. Is there a way to avoid this behavior and act like the native alert, without changing the function signature?







javascript jquery sweetalert sweetalert2






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 16:32









Ademilson Santana da SilvaAdemilson Santana da Silva

1062




1062













  • it is a js window.location.href

    – Ademilson Santana da Silva
    Nov 13 '18 at 17:06



















  • it is a js window.location.href

    – Ademilson Santana da Silva
    Nov 13 '18 at 17:06

















it is a js window.location.href

– Ademilson Santana da Silva
Nov 13 '18 at 17:06





it is a js window.location.href

– Ademilson Santana da Silva
Nov 13 '18 at 17:06












1 Answer
1






active

oldest

votes


















0














The difference is that the alert() is modal. This means it blocks all other input and output until it's dismissed. The Sweetalert is not.



You can make it behave in a similar manner by using a callback function which you execute when the OK button is clicked in the Sweetalert. You can do that by passing the function to alert(), then calling it in the then() block, like this:



function alert(message, title = 'Test', type = 'info', callback) {
if (typeof(swal) != 'undefined') {
swal({
html: message,
title: title,
type: type,
width: '24rem',
}).then(() => {
callback && callback();
});
} else {
alert(message);
callback && callback();
}
}

// example usage:
alert('foo bar', 'title', 'info', function() {
window.location.assign('somewhere_else.php');
});





share|improve this answer
























  • ok, but in this case i would have to change all my alerts to pass this callback. I am trying to avoid this because i have too many alerts to change.

    – Ademilson Santana da Silva
    Nov 13 '18 at 17:06











  • In which case it's not really possible to do what you require.

    – Rory McCrossan
    Nov 13 '18 at 17:08











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285512%2fpreventdefault-sweetalert2%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














The difference is that the alert() is modal. This means it blocks all other input and output until it's dismissed. The Sweetalert is not.



You can make it behave in a similar manner by using a callback function which you execute when the OK button is clicked in the Sweetalert. You can do that by passing the function to alert(), then calling it in the then() block, like this:



function alert(message, title = 'Test', type = 'info', callback) {
if (typeof(swal) != 'undefined') {
swal({
html: message,
title: title,
type: type,
width: '24rem',
}).then(() => {
callback && callback();
});
} else {
alert(message);
callback && callback();
}
}

// example usage:
alert('foo bar', 'title', 'info', function() {
window.location.assign('somewhere_else.php');
});





share|improve this answer
























  • ok, but in this case i would have to change all my alerts to pass this callback. I am trying to avoid this because i have too many alerts to change.

    – Ademilson Santana da Silva
    Nov 13 '18 at 17:06











  • In which case it's not really possible to do what you require.

    – Rory McCrossan
    Nov 13 '18 at 17:08
















0














The difference is that the alert() is modal. This means it blocks all other input and output until it's dismissed. The Sweetalert is not.



You can make it behave in a similar manner by using a callback function which you execute when the OK button is clicked in the Sweetalert. You can do that by passing the function to alert(), then calling it in the then() block, like this:



function alert(message, title = 'Test', type = 'info', callback) {
if (typeof(swal) != 'undefined') {
swal({
html: message,
title: title,
type: type,
width: '24rem',
}).then(() => {
callback && callback();
});
} else {
alert(message);
callback && callback();
}
}

// example usage:
alert('foo bar', 'title', 'info', function() {
window.location.assign('somewhere_else.php');
});





share|improve this answer
























  • ok, but in this case i would have to change all my alerts to pass this callback. I am trying to avoid this because i have too many alerts to change.

    – Ademilson Santana da Silva
    Nov 13 '18 at 17:06











  • In which case it's not really possible to do what you require.

    – Rory McCrossan
    Nov 13 '18 at 17:08














0












0








0







The difference is that the alert() is modal. This means it blocks all other input and output until it's dismissed. The Sweetalert is not.



You can make it behave in a similar manner by using a callback function which you execute when the OK button is clicked in the Sweetalert. You can do that by passing the function to alert(), then calling it in the then() block, like this:



function alert(message, title = 'Test', type = 'info', callback) {
if (typeof(swal) != 'undefined') {
swal({
html: message,
title: title,
type: type,
width: '24rem',
}).then(() => {
callback && callback();
});
} else {
alert(message);
callback && callback();
}
}

// example usage:
alert('foo bar', 'title', 'info', function() {
window.location.assign('somewhere_else.php');
});





share|improve this answer













The difference is that the alert() is modal. This means it blocks all other input and output until it's dismissed. The Sweetalert is not.



You can make it behave in a similar manner by using a callback function which you execute when the OK button is clicked in the Sweetalert. You can do that by passing the function to alert(), then calling it in the then() block, like this:



function alert(message, title = 'Test', type = 'info', callback) {
if (typeof(swal) != 'undefined') {
swal({
html: message,
title: title,
type: type,
width: '24rem',
}).then(() => {
callback && callback();
});
} else {
alert(message);
callback && callback();
}
}

// example usage:
alert('foo bar', 'title', 'info', function() {
window.location.assign('somewhere_else.php');
});






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 16:42









Rory McCrossanRory McCrossan

242k29207245




242k29207245













  • ok, but in this case i would have to change all my alerts to pass this callback. I am trying to avoid this because i have too many alerts to change.

    – Ademilson Santana da Silva
    Nov 13 '18 at 17:06











  • In which case it's not really possible to do what you require.

    – Rory McCrossan
    Nov 13 '18 at 17:08



















  • ok, but in this case i would have to change all my alerts to pass this callback. I am trying to avoid this because i have too many alerts to change.

    – Ademilson Santana da Silva
    Nov 13 '18 at 17:06











  • In which case it's not really possible to do what you require.

    – Rory McCrossan
    Nov 13 '18 at 17:08

















ok, but in this case i would have to change all my alerts to pass this callback. I am trying to avoid this because i have too many alerts to change.

– Ademilson Santana da Silva
Nov 13 '18 at 17:06





ok, but in this case i would have to change all my alerts to pass this callback. I am trying to avoid this because i have too many alerts to change.

– Ademilson Santana da Silva
Nov 13 '18 at 17:06













In which case it's not really possible to do what you require.

– Rory McCrossan
Nov 13 '18 at 17:08





In which case it's not really possible to do what you require.

– Rory McCrossan
Nov 13 '18 at 17:08


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285512%2fpreventdefault-sweetalert2%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine