function xdr(url, method, data, callback, errback) { var req; if (!callback) callback = function(){}; if (!errback) errback = function(){}; if(XMLHttpRequest && ('withCredentials' in (req = new XMLHttpRequest()))) { req.open(method, url, true); req.withCredentials = "true"; req.onerror = errback; req.onreadystatechange = function() { if (req.readyState === 4) { if (req.status >= 200 && req.status < 400) { callback(req.responseText); } else { errback(new Error('Response returned with non-OK status'), req); } } }; req.setRequestHeader("Accept", "application/json"); if (data) req.setRequestHeader("Content-type", "application/json"); req.send(data); } else if(XDomainRequest) { var reqUrl = url; if (url.indexOf('http:') === 0) { reqUrl = url.substr(5); } else if (url.indexOf('https:') === 0) { reqUrl = url.substr(6); } req = new XDomainRequest(); req.open(method, reqUrl); req.onerror = function(err) { errback(err, req); }; req.onload = function() { callback(req.responseText); }; req.ontimeout = function() {}; req.onprogress = function() {}; req.timeout = 0; req.send(data); } else { errback(new Error('CORS not supported')); } }; function jsonGET(url, callback, errback){ xdr(url, 'GET', null, function(data){ callback(JSON.parse(data)); }, errback); } function jsonPOST(url, data, callback, errback){ xdr(url, 'POST', data, function(data){ callback(JSON.parse(data)); }, errback); } function startGallery(items, index) { var pswpElement = document.querySelectorAll('.pswp')[0]; var options = { index: index || 0 }; // Initializes and opens PhotoSwipe var gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options); gallery.init(); return gallery; }