echo "<html>";
echo "<head>";
echo "<script>";
echo " function logout()";
echo " {";
echo " var r=confirm(\"Are you sure you want to logout?\");";
echo " if (r==true){window.location.href=\"http://www.google.com\";}";
echo " }";
echo "</script>";
echo "</head>";
echo "<body>";
echo "<div ALIGN=\"right\" onclick=\"logout();\"> <a href=\"\">Logout </a> </div>";
echo "</body>";
echo "</html>";
From the above code, I just want to redirect the user to www.google.com
once the user click "YES" in the confirm box. I tried to alert right after (r == true
), it works, however the page doesn't go to www.google.com
. May I know what I've missed out?
Well,I think you should add same code like this:echo "<div ALIGN=\"right\" onclick=\"logout();\"><a href=\"javascript:return;\">Logout </a> </div>";
because the page has refreshed after the click event bind on DIV element triggered.
This will work if you have Jquery and Jquery UI
echo "<html>";
echo "<head>";
echo "<script>";
echo '$( "#dialog-logout" ).dialog({
autoOpen: false,
resizable: false,
height:160,
modal: true,
buttons: {
"Logout": function() {
location.href = "http://www.google.com";
$( this ).dialog( "close" );},
Cancel: function() {
$( this ).dialog( "close" );}}});
$( "#logout" )
.click(function() {
$( "#dialog-logout" ).dialog( "open" );});'
echo "</script>";
echo "</head>";
echo "<body>";
echo "<div ALIGN=\"right\" id=\"logout\"> <a href=\"\">Logout </a> </div>";
echo '<div id="dialog-logout" title="Logout">
<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>You are about to logout. Are you sure?</p>
</div>';
echo "</body>";
echo "</html>";