window.location.href未被修改

时间:2017-06-30 08:05:04

标签: javascript assignment-operator

我有以下代码

    var windowHref = window.location.href;
    if (windowHref.includes('/project/')) {
        var splitURL = windowHref.split('/');
        window.location.href = windowHref.substring(0, windowHref.indexOf('#/project')) + '?projectId=' + splitURL[splitURL.length - 2];
    }
    window.location.reload(true);

在执行上述代码之前,window.location.href的值为

https://localhost:44301/default.aspx#/project/16a76abd-5b5b-4c63-822f-2bfd7f133adc/home

并且在执行之后,我希望它的值类似于

https://localhost:44301/default.aspx?projectId=16a76abd-5b5b-4c63-822f-2bfd7f133adc

但是当行

window.location.href = windowHref.substring(0,windowHref.indexOf('#/project')) + '?projectId=' + splitURL[splitURL.length - 2];

执行

window.location.href保持不变。

的结果

windowHref.substring(0,windowHref.indexOf('#/project')) + '?projectId=' + splitURL[splitURL.length - 2];

https://localhost:44301/default.aspx?projectId=16a76abd-5b5b-4c63-822f-2bfd7f133adc

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我建议删除行var href = window.location.href; var index = href.indexOf('#/project/'); if(index!==-1){ var splitURL = href.substring(index,href.length).replace('#/project/','?projectId=').split('/'); window.location.href = splitURL[0]; } ,因为在使用window.location.href

时,无需将客户端重定向到新页面
            JSONObject json = new JSONObject();
            json.put("requestType", "xxxx");
            json.put("projectToken", "xxxxxxxxxxxxxx");
            StringEntity params = new StringEntity(json.toString());
            post.setEntity(params);