DevStarlight
DevStarlight

Reputation: 804

read variables from a parent tab

I'm trying to upload a video meanwhile the user navigates through my web application.

To do so, I'm trying to open a new tab in the background with a simple page that includes a new javascript library that just upload the video passing all the parameters it needs.

Right now everything's in 1 library, so I don't see the way I can create that new thread I'm looking for.

Here below I paste the code I'm using:

var blob;

function xhr(url, data, callback) {
    'use strict';

    var request = new XMLHttpRequest();
    request.onreadystatechange = function () {
        if (request.readyState === 4 && request.status === 200) {
            callback(request.responseText);
        }
    };
    request.open('POST', url);
    request.send(data);
}

function sendVideo(name, path) {
    'use strict';

    var formData = new FormData();

    formData.append('filename', name + '.webm');
    formData.append('video', blob);
    formData.append('title', $('.title').val());
    formData.append('tag1', $('#tag1').val());

    if ($('#tag2').val()) {
        formData.append('tag2', $('#tag2').val());
    }

    if ($('#tag3').val()) {
        formData.append('tag3', $('#tag3').val());
    }

    xhr(path, formData, function (fName) {
        if (fName === 'success') {
            window.alert('Your video has been succesfully uploaded');
        } else {
            window.console.log(fName);
        }
    });
}

<div onclick="sendVideo(1, 'test');">Send</div>

As well, I'm open for new suggestions about different ways to deal with this problem.

Thanks in advice.

Upvotes: 1

Views: 42

Answers (1)

Bardo
Bardo

Reputation: 2523

It seems a perfect fit for web workers.

Web workers are a fairly new tool in HTML5 that allows you to "use" multithreading along your pages.

I've used them to acomplish big file loads in the background.

Check this wonderful article from Eric Bidelman on the subject for further info: http://www.html5rocks.com/en/tutorials/file/filesystem-sync/

Upvotes: 1

Related Questions