Cameron
Cameron

Reputation: 28783

Using window.location and return confirm together

I have this button that will send a user to a page but first ask the user to confirm, but it's not working correctly, how can I fix it? Thanks

<input class="cancel" type="button" value="Cancel" onclick="window.location.href='<?php bloginfo('home'); ?>';return confirm('Are you sure you want to delete your post?');" />

Upvotes: 3

Views: 19965

Answers (3)

Daniel A. White
Daniel A. White

Reputation: 190905

Write a new function

void redirect(url) {
  if (confirm('Are you sure you want to delete your post?')) {
    window.location.href=url;
  }
  return false;
}

then from your onclick

onclick="redirect('<?php bloginfo('home'); ?>');"

Upvotes: 5

simshaun
simshaun

Reputation: 21466

Try this:

<input class="cancel" type="button" value="Cancel" onclick="if (confirm('Are you sure you want to delete your post?')) window.location.href='http://www.google.com';" />

Upvotes: 10

Pekka
Pekka

Reputation: 449395

Turn the commands around. You are leaving the current page, then doing the confirm. That can't work.

if (confirm(...))  window.location.href='....'

Upvotes: 1

Related Questions