重定向确认为真

我想这样做,以便用户点击链接,他们会得到一个确认框。 如果他们单击确定,他们会被发送到一个新的PHPfunction。

我试过了:

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:非法字符
}一个€<

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

没有string方法。 它给出了一个错误。 还要考虑在函数中添加return false以防止默认链接操作。

HTML:

 Test 

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 }​ 

演示: http //jsfiddle.net/xKhaq/