I got a script that puts urls into one iframe by order:
<script type="text/javascript">
$(document).ready(function(){
var array = ['http://www.example1.come', 'http://www.example2.com', 'http://www.example3.com'];
var beforeLoad = (new Date()).getTime();
var loadTimes = [];
$('#1').on('load', function() {
loadTimes.push((new Date()).getTime());
$('#1').attr('src', array.pop());
if (array.length === 0) {
$.each(loadTimes, function(index, value) {
$("#loadingtime"+index).html(value - beforeLoad);
});
}
}).attr('src', array.pop());
});
</script>
I got a txt file with a list of urls. The urls in that file are written in a column (1 column, each url starfs from new line). How can i replace the 'var array' in the javascript with the content of that txt file?
2 Answers 2
This is the basic gist, you'll want to embed any code that you want to run after the file is retrieved where I placed the comment.
$.get("your_url",
function(data) {
var array = data.split(/\r\n|\r|\n/) //regex to split on line ending
var beforeLoad = (new Date()).getTime();
var loadTimes = [];
//.... rest of your code here
}
);
Note that this doesn't do any error handling if the file isn't there, and you may want to rewrite it to use jQuery's "Promise" interface described here.
1 Comment
Use jQuery's get method to request the file directly from the server, or use get to call a php script that parses the file into a friendly format such as JSON.
$.get('file.txt');
If you parse the url's into JSON format, then when it is received you can easily use the urls.
$.get('file.txt', function(data){
obj = JSON.parse(data);
// ....
});