0

I have an array of URLs that i need to find the redirects for. I have been using XMLHttpRequest / xhr.responseURL to do so. When I print the results to the console, the redirected URLs display as expected. However, when I try to save those redirected URLs to an array, the array remains empty. How can I save them to the array?

Updated with code

var imageDestinations = [];

function imageDestinationGrabber(imageSource) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', imageSource, true);
    xhr.onload = function() {
    imageDestinations.push(xhr.responseURL).trim());
    console.log((xhr.responseURL).trim());
    };
    xhr.send(null);
}

The console log works, but the array remains empty.

ZDROM
  • 1
  • 1
  • 1
    you should post the relevant code – Collin Mar 09 '16 at 21:01
  • Not sure if this is the cause of a problem but parentheses at line `imageDestinations.push(xhr.responseURL).trim());` doesn't look right maybe it should be `imageDestinations.push(xhr.responseURL.trim());` – Tomas Mar 09 '16 at 21:18

1 Answers1

2

You had a couple syntax issues that was breaking your code. You had an extra parenthesis at the end of your array push.

imageDestinations.push(xhr.responseURL).trim());

That is trying to .trim() the .push() call

Here's the fixed code:

var imageDestinations = [];

function imageDestinationGrabber(imageSource) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', imageSource, true);
    xhr.onload = function() {
        imageDestinations.push( xhr.responseURL.trim() );
     console.log( xhr.responseURL.trim() );
 };
 xhr.send(null);
}
Adrian Gonzales
  • 1,010
  • 6
  • 8