This is the sample code <script language=javascript> var width = 640; var height = 480; var imgAr1 = new Array(); var rImg1 = new Array(); //Assuming the content of the folder are 4 files imgAr1[0] = "photos2/snow.jpg"; imgAr1[1] = "photos2/snow2.jpg"; imgAr1[2] = "photos2/snow3.jpg"; imgAr1[3] = "photos2/snow4.jpg"; </script>
ok tnx.. i have a code in php getting the filenames of a directory my problem is how to pass to a javascript.. $string=""; $fileCount=0; $filePath=$PATH.'photosheader/'; # Specify the path you want to look in. $dir = opendir($filePath); # Open the path while ($file = readdir($dir)) { if (eregi("\.jpg",$file)) { # Look at only files with a .jpg extension $string .= "'$file'," ; $fileCount++; } }
Yes, I think it can only be done with server side code. Javascript is used more on the client side [browsers]. JSP, AJAX, are other examples of server side languages which can provide information about directory structure. Q.
If you've already got the filenames using PHP, you should be able to create a JavaScript array. Just echo the JavaScript defining the array out to the web page: <?php echo "<script type=\"text/javascript\">\n"; echo "jsArray.push($phpArray[0]);\n"; echo "jsArray.push($phpArray[1]);\n"; echo "jsArray.push($phpArray[2]);\n"; ... echo "</script>\n"; ?> Code (markup): Let me know if that works.