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

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -