Posted By

Piotrek290 on 01/19/10


Tagged

ajax javascript file exists head


Versions (?)

file_exists


 / Published in: JavaScript
 

  1. function file_exists (url) {
  2. // Returns true if filename exists
  3. //
  4. // version: 909.322
  5. // discuss at: http://phpjs.org/functions/file_exists // + original by: Enrique Gonzalez
  6. // + input by: Jani Hartikainen
  7. // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  8. // % note 1: This function uses XmlHttpRequest and cannot retrieve resource from different domain.
  9. // % note 1: Synchronous so may lock up browser, mainly here for study purposes. // * example 1: file_exists('http://kevin.vanzonneveld.net/pj_test_supportfile_1.htm');
  10. // * returns 1: '123'
  11.  
  12. var req = this.window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
  13. if (!req) {throw new Error('XMLHttpRequest not supported');}
  14. // HEAD Results are usually shorter (faster) than GET
  15. req.open('HEAD', url, false);
  16. req.send(null);
  17. if (req.status == 200){ return true;
  18. }
  19.  
  20. return false;
  21. }

Report this snippet  

You need to login to post a comment.