重定向时确认为真 [英] Redirecting on confirm true

查看:93
本文介绍了重定向时确认为真的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要这样做,以便用户单击一个链接,他们会得到一个确认框.如果单击确定",则会将它们发送到新的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:非法字符
}â€

这是说â‹在函数末尾的}之后.我正在外部获取此脚本,因此,如果您发布答案,请确保它在外部可用.不只是在html文件本身中.香港专业教育学院有没有的答案.谢谢!

解决方案

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

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

I want to make it so the user clicks a link, they get a confirm box. If they click OK, they get sent to a new php function.

I've tried:

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 error says:

SyntaxError: illegal character
}​

It's saying that ​ is after the } at the end of the function. I'm getting this script externally so if you post an answer make sure it works externally. Not just in the html file itself. Ive had answers that dont. Thanks!

解决方案

There is no string method. It gives an error. Also consider adding return false to your function in order to prevent the default link action.

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/

这篇关于重定向时确认为真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆