Checking existance of a file on web server using server-side Javascript -
i generating html code using javascript on web server , html contains object has id "imageid" , src parameter pointing "picture1.jpg".
i want refresh web page every 5 seconds , use javascript generate new file path using incrementing numbers "picture2.jpg" or "picture3.jpg" want use javascript check whether such new file name exists on server before trying refresh img object using
document.getelementbyid("imageid").src="picture2.jpg";
how can server-side javascript used test existence of particular server-side file?
is these simple fileexists() method or object returned document.getelementbyid(imagename) have queried somehow find out if found file or not.
thanks everyone.
you can check if file exists on same server using xmlhttprequest
request , check if response code 200
, i.e.:
<script> var http = new xmlhttprequest(); http.open('head', "https://yourserver/file.jpg", false); http.send(); console.log(http.status); </script>
note: cannot query remote server using xmlhttprequest
if cors isn't setup
Comments
Post a Comment