Is there any way using either html or javascript or anything else,to add all the images present in a folder to the webpage.
Suppose i have 10 images then instead of writing ten lines of code like
<img src="1.jpg"/>
<img src="2.jpg"/>
.
.
.
etc
just a single line of code which could add all images from a folder like "*.jpg" all jpg images. I'm new to web technologies, and just got such doubt.
Any help highly appreciated.
Thanks and best Regards
Is there any way using either html or javascript or anything else,to add all the images present in a folder to the webpage.
Suppose i have 10 images then instead of writing ten lines of code like
<img src="1.jpg"/>
<img src="2.jpg"/>
.
.
.
etc
just a single line of code which could add all images from a folder like "*.jpg" all jpg images. I'm new to web technologies, and just got such doubt.
Any help highly appreciated.
Thanks and best Regards
Share Improve this question asked Dec 12, 2010 at 15:14 technocrattechnocrat 7355 gold badges13 silver badges23 bronze badges 1- The problem with JS is it is client side, and to retrieve the files in a folder you will need to have some sort of index which will have to be generated server side. – Tom Gullen Commented Dec 12, 2010 at 17:15
3 Answers
Reset to default 5In HTML - no.
But, if all images are named as "1.jpg", "2.jpg", "3.jpg"... so you can build a javascript loop.
try something like:
<html>
<body>
<script type="text/javascript">
var i=0;
for (i=0;i<=5;i++)
{
document.write("<img src=\"" + i + ".jpg\"/>");
document.write("<br />");
}
</script>
</body>
</html>
Read here: http://www.w3schools./JS/js_loop_for.asp ,and try learning basic programming issues, just as looping..
As you said "or anything else" I would remend using a bash one-liner for collecting images in the current folder and add them to a single file, named index.html:
for i in *.jpg; do echo "<img src='$i' />" >> index.html; done;
Just add the missing html-tags and done.
EDIT: You can use the Windows shell (cmd) as well:
FOR %i IN (*.JPG) DO ECHO ^<img src="%i" /^> >> index.html
It's much easier to do this with a server-side technology, such as PHP with its glob()
function and foreach
loop.