Reputation: 616
I have to display a sequence of images as a slideshow. I will have the path of the images in a text file. How read the image path from a text file ? Right now I have a hardcoded code like below:
<div class="mySlides fade">
<div class="numbertext">1 / 3</div>
<img src="img_nature_wide.jpg" style="width:100%">
<div class="text">Caption Text</div>
</div>
Example text file with image paths:
https://res.cloudinary.com/demo/image/upload/sample.jpg https://res.cloudinary.com/demo/image/upload/v1/folder1/folder2/sample.jpg
Upvotes: 0
Views: 1942
Reputation: 2158
In regards to my comment, you request a URL and then parse the response and do something with it not with HTML but with Javascript.
We start by waiting for the document to load.
Fetch Documentation: Fetch
// When document is loaded and ready
$(document).ready(function(){
// On button click initiate
$("button").click(function(){
// Request URL, change to http://example.com/file.txt
fetch('https://api.ipify.org?format=json')
.then(response => response.text())
.then(data => {
// Log body text
console.log(data);
// Set #results text to body text
$('#results').text(data);
})
});
});
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<div id="results"></div>
<button>Get External Content</button>
</body>
</html>
Upvotes: 1
Reputation: 447
First you must understand that javascript alone can't access other files in your server there will always be a need for php weather you implement ajax or not so here is a php example asssuming you have a text file urls.txt
with the following contents
https://res.cloudinary.com/demo/image/upload/sample.jpg https://res.cloudinary.com/demo/image/upload/v1/folder1/folder2/sample.jpg
//Get the contents of the text file
$text = file_get_contents("path/to/urls.txt");
//Split them by the seperate lines
$textArr = explode("\n",$text);
//loop through the array
for($i = 0; $i < count($textArr); $i++){
//Echo the images
echo "<img src='" . $textArr[$i] . "' style='width:100%'>";
}
Upvotes: 1
Reputation: 8730
The easiest way would probably be to store your urls in a JSON
encoded file, and use fetch
to retrieve it.
Imagine, you have the following JSON
file in your server :
"[
"https://placekitten.com/408/287",
"https://placekitten.com/200/286",
"https://placekitten.com/200/139"
]"
You can retrieve the file using fetch
, and operate with the resulting array
of urls, to populate your slideshow :
fetch('http://example.com/yourFile.json')
.then(function(response){
// parse the fetch response
return response.json();
})
.then(function(myJson){
// do whatever you need with the image paths array...
console.log(myJson);
});
Upvotes: 1