tags:

views:

163

answers:

3

I want to make a YAHOO.util.Connect.asyncRequest call, which not is async. Just like open(method, url, async) where false is passed by async.

I can't find a "syncRequest" in the Connect class. Is this possible using YUI 2?

I tried without YUI instead:

function createRequestObject() {
    var ro;
    // Mozilla, Safari,...
    if (window.XMLHttpRequest) {
        ro = new XMLHttpRequest();
        if (ro.overrideMimeType) {
            ro.overrideMimeType('text/xml');
            // See note below about this line
        }
        // IE
    } else if (window.ActiveXObject) {
        try {
            ro = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                ro = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }
    if (!ro) {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
    return ro;
}
function sndReq(param,server,handler) {
    http = createRequestObject();
    http.open('GET', server+"?"+param, false);
    http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    http.onreadystatechange = handler;
    http.send(null);
}

But in FireFox and Safari the callback function (handler) is not called on 'onreadystatechange' when false is passed in 'open'? In IE and Opera it works ok.

+1  A: 

YUI2 doesn't support synchronous requests. According to one of the devs in IRC it will eventually be part of YUI3. They aren't in a huge rush to support it though due to the havoc it plays with the user experience.

Tivac
+2  A: 

YUI3 supports synchronous requests in "YUI io" and is available on development master on GitHub. The implementation syntax is included in the README file, and will be formally documented for release in 3.1.0. This enhancement is documented as 2528181 on yuilibrary.com.

If you are in a position to use YUI 3, give io a try.

tsha
+1  A: 

Use JQuery. jQuery.ajax({async:false}) works in both IE and FF.

Martin Hoegh