javascript - Checking if local files are available -
<script> var test; function gameexists() { var http = new xmlhttprequest(); http.open('head', "/flappy.html", false); http.send(); if(http.status!=404){ test= "true"; }else{ test == "false"; } } </script> <script> if(test=="true") { alert('game exists, add code.'); } else { alert('download game !'); $('#frame').append('<a href="path/to/flappy.html">download</a>');} </script>
this javascript, check if local file available. being hosted locally can not use server side languages. not sure have done wrong. please me, make work. a) doesn't check correctly b) doesn't download . thank :) not sure why code not working, please me out! doing wrong?
couple of issues:
1) you're trying access /flappy.html
, on local machine, not want - it's literally root of machine. you're looking relative path, use flappy.html
. main issue here.
2) don't invoke function. need call gameexists()
before check result of test
3) use true
, false
, not "true"
, "false"
.
4) should test = false
not test == "false"
. first assignment, second comparison.
you may interested in using jquery's ajax instead of xmlrequest, since have included jquery already. you'll need use callback run bottom half of code, jsonp, can make cross-domain requests.
Comments
Post a Comment