最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using window.location and return confirm together - Stack Overflow

programmeradmin0浏览0评论

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

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

Share Improve this question asked Feb 16, 2011 at 19:24 CameronCameron 28.8k102 gold badges288 silver badges490 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 10

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';" />

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

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

if (confirm(...))  window.location.href='....'
发布评论

评论列表(0)

  1. 暂无评论