0

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?

asked Feb 24, 2013 at 19:54

2 Answers 2

3

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.

answered Feb 24, 2013 at 20:17
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for accounting for all the different possible line endings.
0

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);
 // ....
});
answered Feb 24, 2013 at 20:12

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.