문제

I have a hidden frame that Accepted message, but I do not have to send by clicking. I'm calling the function that performs and actions, including sending a message. Function settimeout not satisfied by their speed.

this.request = function (requestData) {
    $(function () {
        dataOrign = requestData;
        var iframe = $('iframe#postFrame');
        setTimeout(function () {
            var parseData = JSON.stringify(requestData);
            iframe[0].contentWindow.postMessage({ request: parseData }, '*');
        }, 1000);
    });
};

How can i send postMessage with minimal time? In this situation?

도움이 되었습니까?

해결책

For most browsers, you can listen for the load event (some example found here) for that iframe to do the postMessage.

E.g.

$('iframe#postFrame').on('load', function(e) { 
    // do postMessage here
});

However it appears that the setTimeout/setInterval method is unavoidable for older browsers (IE7/8).

UPDATE

If you would like to write it as a function, you could do this:

var request = function(requestData, $iframe) { 
    if ($iframe.length > 0 && $iframe.get(0).document.readyState === 'completed') { 
        // do postMessage
    }
};

Then you can just use it this way:

request(data, $('iframe#postFrame'));
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top