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.
Javascript confirm box, if cancel is clicked, it will not reload the page
Asked Answered
Sure, there are a lot of ways –
Calvados
if ( confirm("RELOAD?") ) location.reload(); –
Jephthah
I mean it will not reload if you clicked cancel. @Jephthah –
Daladier
Can you teach me one way how? @Calvados –
Daladier
it does not - if you click cancel, confirm returns false, and location.reload() doesn't execute –
Jephthah
i've tried it, it still reload the current page.@Jephthah –
Daladier
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.........
it still reload the current page if i clicked cancel –
Daladier
if (r==true)
is redundant. It functions the same as if (r)
–
Elohim @Elohim thanks i got your point...edited my answer... user3203544 if you show us your code may we help you more....without code we can't get your point.. –
Hortensiahorter
@user3203544 May I ask what browser you are using? Kuldeep's code works fine for me in Chrome. –
Elohim
This is my code: function logout(){ var r = confirm("Logging out during the exam means you'll get a FAILED remark."); if (r==true){ window.location.href = "failed.php"; return false; } else{ } } then i have a button <button onclick="return logout()">lougout</button> –
Daladier
i uses both firebox or crome, it works fine in all browser, it's javascript code ,i thinks it works fine in every browser....... –
Hortensiahorter
Im using chrome also. @Elohim When I clicked cancel, the current page will reload. –
Daladier
@user3203544 In your
onclick
remove the return. It should just be onclick="logout()"
–
Elohim It reloaded still. :(( I dont know what to do. @Elohim –
Daladier
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
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>
it still reload the current page if i clicked cancel –
Daladier
After press the cancel, the page will not reload, please have a look updated answer. –
Lamoreaux
When i click cancel, an alert box appeared then when i clicked OK on the alert box, it reloaded. –
Daladier
I have run this code into my local server but's working fine, it's not reloaded after press on cancel. There is something else. Please update question with relative code. –
Lamoreaux
Can you just give me a complete code? with the ONCLICK event. Please. –
Daladier
I've tried it. Still, the current page reloaded. The reason when i dont what to reload the page because im creating a quiz. the answers in the page are also removed when it is reloading. –
Daladier
The code I provided is fine, to know what exactly is going on, you need to show more code from your side. –
Lamoreaux
I know the problem. The reason why it is reloading, is the while loop sql code for the questions in my quiz. How can i solve it? –
Daladier
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;
}
}
So nice of you to answer this however, The
None
in else block will results in error. –
Attending © 2022 - 2025 — McMap. All rights reserved.