javascript - Redirecting on confirm true - Stack Overflow

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

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!

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!

Share Improve this question edited Jun 2, 2012 at 14:47 Dylan Buth asked Jun 2, 2012 at 13:47 Dylan ButhDylan Buth 1,6667 gold badges36 silver badges58 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

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/xKhaq/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744143932a4560332.html

相关推荐

  • javascript - Redirecting on confirm true - Stack Overflow

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

    8天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信