AJAX CSV URL Loader

jQuery/JS Loads lots of URLs (grabbed from a CSV) in an iframe via AJAX

by Preston Badeer

HTML

<textarea id="thedata" rows="10" cols="50">
        http://asdf.com, http://microsoft.com, http://w3schools.com, http://prestonbadeer.com, http://hayneedle.com, http://hannahwittwer.com, http://evernote.com
    </textarea><br/>

    <input type="submit" id="sub" onclick="loader()" style="width:100px;height:50px;"/>

<br/>

<iframe id="change" src="" width="1024" height="500"></iframe>

JavaScript

// Prototype function for safe splitting of csv files, accounts for commas within values
// from http://www.greywyvern.com/?post=258
String.prototype.splitCSV = function(sep) {
  for (var foo = this.split(sep = sep || ","), x = foo.length - 1, tl; x >= 0; x--) {
    if (foo[x].replace(/"\s+$/, '"').charAt(foo[x].length - 1) == '"') {
      if ((tl = foo[x].replace(/^\s+"/, '"')).length > 1 && tl.charAt(0) == '"') {
        foo[x] = foo[x].replace(/^\s*"|"\s*$/g, '').replace(/""/g, '"');
      } else if (x) {
        foo.splice(x - 1, 2, [foo[x - 1], foo[x]].join(sep));
      } else foo = foo.shift().split(sep).concat(foo);
    } else foo[x].replace(/""/g, '"');
  } return foo;
};


// URL Loader (process the image URLs but don't check for success)
function loader(){
    urls = document.getElementById('thedata').value.splitCSV();
    l = urls.length;
    i = 0;
    
    $('#change').load(function(){
        if(i == l){
            alert('Done!');
        }

        if(i < l){
            $('#change').attr('src', urls[i]);
            i++;
        }
    });

    $('#change').attr('src','http://hayneedle.com');
}