window.onbeforeunload并重定向到根路径

时间:2011-07-13 07:56:24

标签: javascript ajax jsp redirect activex

我确实在项目中找到了一些javascript / activex代码,在离开页面时调用(window.onbeforeunload):

我的项目可在地址

处访问
www.someaddress.itdoesntexists/MyProjectName/page.jsp

当调用logout函数时,页面logout.jsp中的操作已正确执行,但在该过程结束时,用户被重定向到

www.someaddress.itdoesntexists

而不是

ww.someaddress.itdoesntexists/MyProjectName/

代码:

<script type="text/javascript">
var loggedout = false;

bVer = parseInt(navigator.appVersion);
bName = navigator.appName;
browserIE = bName == "Microsoft Internet Explorer";
browserNS = bName == "Netscape";

function sendHttpRequestSubmit (http_request, parameters) {
    http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http_request.setRequestHeader("Content-length", parameters.length);
    http_request.setRequestHeader("Connection", "close");
    http_request.send(parameters);
}

function httpRequest(url, mime, callback, async, parameters) {
    var http_request = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) http_request.overrideMimeType(mime);
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }
    if (!http_request) {
        alert('Unable to create a XMLHTTP instance');
        return false;
    }
    http_request.onreadystatechange = function () {
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
                if (callback != null) callback(http_request);
            } else alert('There is a problem with the request "' + url + '"');
        }
    }
    async = async == null ? true : async;
    http_request.open('POST', url, async);
    if (parameters != null) sendHttpRequestSubmit(http_request, parameters);
    else http_request.send(null);
    if (browserNS && !async) {//
        if (callback != null) callback(http_request);
    }
}

function logout () {
    var sg;
    if (!loggedout)
        httpRequest ("logout.jsp?js=1", "text/javascript", function (http_request) {
            sg = eval(http_request.responseText);
        }, false);
    loggedout = true;
    return sg;
}

window.onbeforeunload = logout;

有人可以向我解释在哪里告诉脚本它不必转到根路径吗?

1 个答案:

答案 0 :(得分:1)

该脚本不直接声明重定向 - 它只是通过将其作为函数进行评估来处理来自对logout.jsp?js=1的AJAX调用的响应 - 我猜你需要修改该响应文本(所以在你发布的脚本之外),让它重定向到你想要的位置。