user3397013
user3397013

Reputation: 1

Make an array in Javascript with images from a folder

I want to navigate through images from a folder with JS, and i want to make an array of the files in the folder. I can't figure how to do that...I want to make the array |var images| and get rid of the links that i have manualy put them there.

<html>
<head>
<title>Ranking Page</title>
<script language="Javascript">

var images = [
    "http://dummyimage.com/600x400/000/fff&text=two",

    "http://dummyimage.com/600x400/000/fff&text=one"

    ];
var iIndex;
var iLen = images.length;

function fn_keydown(event) {
    var img = document.getElementById("wrapper").childNodes[1];
    if (event.keyCode === 39) {
        iIndex = (iIndex + 1) >= iLen ? 0 : iIndex + 1;
    } else if (event.keyCode === 37) {
        iIndex = (iIndex - 1) < 0 ? iLen-1 : iIndex - 1;
    }
    img.setAttribute("src", images[iIndex]);
}
window.onkeydown = fn_keydown;
window.onload = function() {
    iIndex = iLen;
    var vEvent = {
            keyCode : 39
        };
    fn_keydown(vEvent);
}

</script>
</head>
<body>
<div id="wrapper">
    <img />
</div>

</body>
</html>

Upvotes: 0

Views: 9313

Answers (1)

Ankur Aggarwal
Ankur Aggarwal

Reputation: 3101

You can't do this with JavaScript as it does not have access to a computer's file system. That is because JavaScript was designed with security in mind.

You need to use server side languages for this like asp or php.You would be able to access the filesystem then with the correct security permissions and build your javaScript array code on the server. When the page loads up the paths to your images will exist in the web page and then you can do what you want with them in your javaScript.

Upvotes: 2

Related Questions