dq_609586475 2014-12-21 14:31
浏览 91
已采纳

JavaScript从target = _blank提交表单中检索窗口句柄

I'm trying to create an automated script that lets me download various files from a website, then upload them to a new website. The script will let multiple users copy content from the old site to the new one at the same time.

I initially attempted to use AJAX to download one file at a time, and store the file(s) data in variables, then upload this variable (file data) to the second domain. The download worked fine, but the PHP page on the second domain always threw back a 500 request error message AFTER uploading, despite setting various file size parameters and processing time parameters in PHP. This meant I couldnt even see what was echoed by the PHP page to debug, and continue developing that method.

After spending a long time trying to fix this (and failing miserably), I decided to just let the users download the old files normally, then use a form to upload the files. As I want the script to continue to run whilst the file is uploading, I've decided to set the forms target to "_blank" to open a new tab (which uploads the file).

How can I retrieve the windows handle that's opened with the target="_blank" form, to test when the window is closed? I don't need to access the windows HTML, I just want to check when it closes. The page that's opened by the form is not on the same domain as the form that opens it, but the "Access-Control-Allow-Origin" header is set to "*" on the second domains PHP page.

  • 写回答

1条回答 默认 最新

  • doutuohan6606 2014-12-21 21:14
    关注

    I don't think you can retrieve the window handle, but you can use Javascript to keep a reference to the window and check whether the window is closed or not. You would have to open your window with Javascript like this

    var uploadWindow = window.open("http://www.google.com");
    

    Then you can use a function on a timer to monitor if the window has closed or not:

    var interval = 250;
    setTimeout(checkClose, interval);
    
    function checkClose() {
        if (uploadWindow.closed) {
            // Code to run when window is closed
        }
        else {
            setTimeout(checkClose, interval);
        }
    }
    

    I hope this can be of help

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部