开发者

window.onbeforeunload and redirect to root path

I did find some javascript/activex code in a project, called when leaving a page (window.onbeforeunload):

My project is reachable at the address

www.someaddress.itdoesntexists/MyProjectName/page.jsp

When the logout function is called, the action in the page logout.jsp is correctly performed but at the end of the process the user is redirected to

www.someaddress.itdoesntexists

instead of

ww.someaddress.itdoesntexists/MyProjectName/

The code:

<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.setRequest开发者_如何学运维Header("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;

Can someone explain to me where to tell the script that it doesn't have to go to the root path?


The script doesn't directly declare the redirect - it simply handles a response from an AJAX call to logout.jsp?js=1 by evaluating it as a function - I would guess that you'll need to modify that response text (so outside of the script you've posted) to get it to redirect to the location you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜