开发者

Second Javascript form POST fails in Chrome, works in IE and FF

开发者 https://www.devze.com 2023-03-23 12:42 出处:网络
I\'m opening a new window via a javascript POST in response to a user click in a flash application. The user may close the new window and want to re-open it by clicking the button on the first page ag

I'm opening a new window via a javascript POST in response to a user click in a flash application. The user may close the new window and want to re-open it by clicking the button on the first page again. Since I have to pass some large arguments to the second page, I have to do a POST, a GET won't work. So far I have the second page opening and behaving correctly in Chrome, FF, and IE the first time the button is clicked. However, in Chrome (it works in IE and FF) the second time the button is clicked, the POST is ignored and the new window is not opened.

Here's the function I use to do the POST. I have verified that it is making it all the way past the form.submit() line in Chrome with all the same parameters and no error notifications, but a new window still doesn't open.

function post_to_url(path, paramString) {
    var params = paramString.split("|");

    var form = document.createElement("form");
    form.setAttribute("method", "post");
    form.setAttribute("action", path);
    form.setAttribute("target", "_blank");

    for (var i=0; i<params.length; i++) {
        var hiddenField = document.createElement("input");
        var param = params[i].split(":");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", param[0]);
        hiddenField.setAttribute("value", param[1]);

        form.appendChild(hiddenField);
    }

    document.body.appendChild(form);
    form.submit();
    document.body.removeChild(form);
}

Any ideas on how this can be modified to get it to work in Chrome also?

Update: It appears that the form isn't removed from the DOM in Chrome. I'm not sure if that's the issue or not, but it should be removed regardless.

We've also noticed that while 2nd POSTs in Chrome on Linux and Windows don't go through, they do on Macs. However, even on a Mac the form isn't removed from the DOM.

Another Update: Altering the code like this correctly removes the form from the DOM, but it doesn't solve the POST problem.

function post_to_url(path, paramString) {
    var postform = document.getElementById("postform");
    if (postform != null)
        document.body.removeChild(postform);
    var params = paramString.split("|");

    var form = document.createElement("form");
    form.setAttribute("method", "post");
    form.setAttribute("action", path);
    form.setAttribute("target", "_blank");
    form.id = "postform";

    for (var i=0; i<params.length; i++) {
        var hiddenField = document.createElement("input");
        var param = params[i].split(":");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", param[0]);
        hiddenField.setAttribute("value", param[1]);

        form.appendChild(hiddenField);
    }

    document.body.appendChild(form);
    form.submit();
    var postform = document.getElementById("postform"); 
    document.body.removeC开发者_如何学Gohild(postform);
}


I found a solution. Adding a random number to the end of the URL causes Chrome to run the POST multiple times. There must be a "feature" in Chrome that prevents multiple POSTs to the same URL.

Here's what I changed:

form.setAttribute("action", path + '?' + Math.random().toString());


Make sure that a pop-up blocker isn't blocking the second open. I had a problem similar to this a little while ago.

0

精彩评论

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

关注公众号