Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes regarding to onFinish event, request credentials and IE9 compatibility #29

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 56 additions & 12 deletions src/vast-vmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,60 @@ var VASTAds, VASTAd, VASTLinear, VASTNonLinear, VASTCompanion;
* identifier.
*/
function fetchXML(url, identifier, onSuccess, onFailure) {
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (request.readyState === 4) {
if (request.status === 200) {
if (request.responseXML !== null) {
onSuccess(request.responseXML, identifier);
var request;

// IE 9 CORS method
if (window.XDomainRequest)
{
request = new XDomainRequest();

request.onload = function()
{

if (request.contentType != null && request.responseText != null)
{

// IE < 10 requires to parse the XML as string in order to use the getElementsByTagNameNS method
var parser = new DOMParser();
var doc = parser.parseFromString(request.responseText, 'text/xml');

onSuccess(doc, identifier);

}
else
onFailure(request, identifier);

};

request.onerror = request.ontimeout = function()
{
onFailure(request, identifier);
};

}
else // The standard one
{

request = new XMLHttpRequest();

request.onreadystatechange = function() {
if (request.readyState === 4) {
if (request.status === 200) {
if (request.responseXML !== null) {
onSuccess(request.responseXML, identifier);
} else {
onFailure(request, identifier);
}
} else {
onFailure(request, identifier);
}
} else {
onFailure(request, identifier);
}
}
};
};

}

request.open("GET", url, true);
request.withCredentials = true; // Accept cookies
request.send(null);
}

Expand All @@ -53,8 +91,9 @@ function fetchXML(url, identifier, onSuccess, onFailure) {
* @param { {function()}} onError Function to call when no ads are fetched
* or there was an error requesting the endpoint
* @param {?VASTAd} parentAd The ad containing the results from this query
* @param {funciton()}} onFinish Fuction to call when request is finished
*/
function queryVAST(endpoint, onFetched, onError, parentAd) {
function queryVAST(endpoint, onFetched, onError, parentAd, onFinish) {
fetchXML(endpoint, null, function(doc) {
try {
new VASTAds(doc, onFetched, onError, parentAd);
Expand All @@ -70,9 +109,14 @@ function queryVAST(endpoint, onFetched, onError, parentAd) {

onError();
}

if (onFinish) onFinish();

}, function (e) {
console.error("Failed to load VAST from '" + endpoint + "':", e);
onError();

if (onFinish) onFinish();
});
}

Expand Down Expand Up @@ -1086,7 +1130,7 @@ function VASTLinear(ad, root) {
this.clickThrough = null;
this.duration = null;
this.adParameters = null;

var i;

var clicks = root.getElementsByTagName("VideoClicks");
Expand Down