重定向确认为真

时间:2012-06-02 13:47:39

标签: javascript jquery confirm

我想让用户点击链接,他们会得到一个确认框。如果他们点击OK,他们会被发送到一个新的PHP功能。

我试过了:

function confirmReset() {
 var r = confirm('Are you sure?');
 var url = window.location.pathname;
 var pathArray = url.split('/');
 var host = pathArray[1];
 var newHost = '/headquarters/designReset';

 if (r == true) {
    window.location = host + newHost;
    //document.location.href = '/headquarters/designReset';
 } else {
    alert('it didnt work');
 }
 return false;
}​

Firebug错误说:

SyntaxError:非法字符
}â€<< / P>

它说 - 在函数结尾处的}之后。我从外部获取此脚本,因此如果您发布答案,请确保它在外部工作。不只是在html文件本身。我有答案,不。谢谢!

1 个答案:

答案 0 :(得分:4)

没有string方法。它给出了一个错误。另外,请考虑将return false添加到您的函数中,以防止默认链接操作。

HTML:

<a href="#" onclick="return confirmReset();">Test</a>​

JavaScript的:

function confirmReset() {
    var r = confirm('Are you sure?');
    var url = window.location.pathname;
    var pathArray = url.split('/');        // <-- no need in "string()"
    var host = pathArray[1];
    var newHost = '/headquarters/designReset';

    if (r == true) {
        window.location = host + newHost;
        //document.location.href = '/headquarters/designReset';
    } else {
        alert('it didnt work');
    }
    return false;                          // <-- add this line
}​

DEMO: http://jsfiddle.net/xKhaq/