简体   繁体   English

PHP JavaScript重定向问题

[英]PHP JavaScript redirect issue

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. 从上面的代码中,我只想在用户单击确认框中的“是”后将用户重定向到www.google.com I tried to alert right after ( r == true ), it works, however the page doesn't go to www.google.com . 我尝试在( r == true )之后立即提醒,它可以正常工作,但是该页面无法访问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. 因为触发DIV元素上的click事件绑定后页面已刷新。

This will work if you have Jquery and Jquery UI 如果您有Jquery和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>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM