1
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?

8
  • Why not use PHP templating? ?><script>//real javascript</script><?php Commented Sep 10, 2013 at 2:34
  • As I've said, I able to alert some text, meaning the way I code it should've no problem with it. I don't want to make too many unnecessary code changes onto current code as the code above is just a small portion of the real code Commented Sep 10, 2013 at 2:36
  • With handlebar template can you doing it, I thing ... Commented Sep 10, 2013 at 2:39
  • @josecarlos: I'm totally lost now. What you mean by using handlebar template. I'm just trying to redirect the user to some other pages which should be very easy, I thought? Commented Sep 10, 2013 at 2:41
  • @IsaacLem: Can't imagine the rest of the code... Doesn it look much nicer like this gist.github.com/elclanrs/6504365? Dunno... if I see that in production I would go crazy! Commented Sep 10, 2013 at 2:43

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.