JavaScript retrieves the window handle from target = _blank.

I am trying to create an automated script that allows me to download various files from a website and then upload them to a new website. The script will allow multiple users to copy content from the old site to the new one at the same time.

I first tried using AJAX to download one file at a time and store the file (s) data in variables, then load that variable (file data) into the second domain. The upload worked fine, but the PHP page on the second domain always sent a 500 request error AFTER upload, even though PHP had various file size and processing time parameters set. This meant that I couldn't even see what the PHP page responded to and continue to develop this method.

After spending a long time trying to fix this (and unfortunately not), I decided to just let users upload old files normally and then use a form to upload files. Since I want the script to keep running while the file is uploading, I decided to set the form target to "_blank" to open a new tab (which uploads the file).

How can I get the handle to the windows opened with the target = "_ blank" form to check when the window is closed? I don't need to access the HTML window, I just want to check when it closes. The page opened by the form is not in the same domain as the form opening it, but the "Access-Control-Allow-Origin" header is set to "*" on the second PHP domain pages.

+3


source to share


1 answer


I don't think you can get the window handle, but you can use Javascript to save a link to the window and check if the window is closed or not. You will need to open your window with Javascript like this

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

      

Then you can use a function on the timer to control if the window is 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 help

+1


source







All Articles