Web Source2015. 7. 27. 14:20

네이버에서는 검색이 어려운 블로그입니다. 구글검색을 이용해 주세요.



http://ask.webatall.com/javascript/9507_how-do-i-check-if-file-exists-in-jquery-or-javascript.html


내가 사용하고자 하는 서버가 살아있는지 확인하는 코드들이다.

파일 또는 URL 을 확인할 때도 쓸 수 있다.

3번이나 7번이 제일 마음에 든다.


var urlok=true;

$('<img src="http://domain.com/img/bc.gif" style="display:none;">').load(function() {

// ok

}).bind('error', function() { //fail

urlok=false;

$('#c').attr('src', 'img/bc_siren.gif').prop('alt','응급용 링크').prop('title','응급용 링크');

});

function openwin() {
var u=urlok?'index.html':'siren.html';
window.open(u, '_blank');
}

<img id='c' onclick='openwin()' src='img/bc_normal.gif' alt='링크' title='링크' style='cursor:pointer;border:none'>



How do I check if file exists in jQuery or JavaScript?

1

How do I check if a file on my server exists in jQuery or JavaScript?

[Related]: How do I check if file exists in jQuery or JavaScript ... How do I check if a file on my server exists in jQuery or JavaScript?
2

With jQuery:

$.ajax({
    url:'http://www.example.com/somefile.ext',
    type:'HEAD',
    error: function()
    {
        //file not exists
    },
    success: function()
    {
        //file exists
    }
});

EDIT:

Here is the code for checking 404 status, without using jQuery

function UrlExists(url)
{
    var http = new XMLHttpRequest();
    http.open('HEAD', url, false);
    http.send();
    return http.status!=404;
}

Small changes and it could check for status HTTP status code 200 (success), instead.

[Related]: How do check if file or image url exists using jQuery or ... Aug 30, 2012 · How do check if file or image url exists using jQuery or Javascript json js ajax ... How do check if file or image url exists using jQuery or Javascript ...
3

This works for me:

function ImageExist(url) 
{
   var img = new Image();
   img.src = url;
   return img.height != 0;
}
[Related]: How Do I Check If File Exists In Jquery Or Javascript User ... Best Place to Download PDF about How Do I Check If File Exists In Jquery Or Javascript and read our other news related to How Do I Check If File Exists In Jquery Or ...
4

A similar and more up-to-date approach.

$.get(url)
    .done(function() { 
        // exists code 
    }).fail(function() { 
        // not exists code
    })
[Related]: How do I check if file exists in jQuery or JavaScript? How do I check if file exists in jQuery or JavaScript? All. Magento; OpenCart; Wordpress; ... REGISTER. LOGIN. Home > How do I check if file exists in jQuery or ...
5

i used this script to add alternative image

function imgError()
{
alert('The image could not be loaded.');
}

HTML:

<img src="image.gif" onerror="imgError()" />

http://wap.w3schools.com/jsref/event_onerror.asp

[Related]: How to check if URL/file exists on input. - jQuery Forum jQuery Support Portal. Loading... () jQuery; Plugins; UI; Meetups; Forum; Blog; About; Donate; All Forums; Recent Posts; Log In; Search jQuery. Search. jQuery Forum
6

So long as you're testing files on the same domain this should work:

function fileExists(url) {
    if(url){
        var req = new XMLHttpRequest();
        req.open('GET', url, false);
        req.send();
        return req.status==200;
    } else {
        return false;
    }
}

Please note, this example is using a GET request, which besides getting the headers (all you need to check weather the file exists) gets the whole file. If the file is big enough this method can take a while to complete.

The better way to do this would be changing this line: req.open('GET', url, false); to req.open('HEAD', url, false);

[Related]: Ambition Lab - How to check if a file exists using JQuery How to check if a file exists using JQuery. ... How to Find & Delete a Specified Value From an Array in JavaScript ; Typeface.js :hover workaround ;
7

I was getting a cross domain permissions issue when trying to run the answer to this question so I went with:

function UrlExists(url) {
$('<img src="'+ url +'">').load(function() {
    return true;
}).bind('error', function() {
    return false;
});
}

It seems to work great, hope this helps someone!

[Related]: Javascript ASK: regex – javascript regexp, can't get ... Jul 30, 2012 · ... javascript regular expression validation to force either to alpha characters or alpha numeric characters but no standalone numbers Saturday PM 15:01:56 ...
8

What you'd have to do is send a request to the server for it to do the check, and then send back the result to you.

What type of server are you trying to communicate with? You may need to write a small service to respond to the request.

[Related]: How do I check if folder exists in jQuery or JavaScript or ... How do I check if folder exists in jQuery or JavaScript or Ajax? How do I check if a folder on my server exists /etc/exmaple/ in jQuery or JavaScript ... HTML5 File ...
9

For a client computer this can be achieved by:

try
{
  var myObject, f;
  myObject = new ActiveXObject("Scripting.FileSystemObject");
  f =   myObject.GetFile("C:\\img.txt");
  f.Move("E:\\jarvis\\Images\\");
}
catch(err)
{
  alert("file does not exist")
}

This is my program to transfer a file to a specific location and shows alert if it does not exist

[Related]: how to check image exist in javascript - SitePoint – Learn ... I've been on the hunt for a simple little function that will detect whether or not an image file exists ... JavaScript & jQuery; how to check ... javascript to check ...
10

Not sure its possible without something serverside going on. It would cause interesting security problems if I could write stuff to interact with your files in javascript. Use PHP for this in my opinion.

[Related]: Check if file exists in Javascript - JavaScript / Ajax / DHTML Jul 20, 2005 · Is there a way to check if a file exists in Javascript? This is what I'm trying to do: if(thisfile.htm exists) do this else do that


Posted by 영육치료