且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用javascript检查服务器上存在的html文件

更新时间:2023-08-21 20:37:52

您可以使用ajax来检查文件是否存在

使用jquery

  $。ajax({
url:'http://www.example.com/3.html',
错误:function()
{
alert('文件不存在');
},
success:function()
{
alert('file exists');
}
});

使用Javascript

 函数checkIfRemoteFileExists(fileToCheck)
{
var tmp = new Image;
tmp.src = fileToCheck;

if(tmp.complete)
alert(fileToCheck +available);
else
alert(fileToCheck +is not available);

现在检查文件是否存在,或者不调用js函数像这样

  checkIfRemoteFileExists('http://www.yoursite.com/abc.html'); 


My ASPX code generated some html files where I just put link for paging like

<a href="1.html">First</a>&nbsp;|&nbsp;
<a href="3.html">Next</a>&nbsp;|&nbsp;
<a href="1.html">Previous</a>&nbsp;|&nbsp;
<a href="9.html">Last</a>

say if user currently on second page when it press Next moves to 3rd page ...

now issue is when user clicking Next button several times and system is in progress to generate let say 5th page it will show error page.

Is there any way to check from html via javascript to check whether file is present or not? Kindly help me to pull out from this show stopper issue

You can use ajax for check file exists or not

Using Jquery

$.ajax({
        url:'http://www.example.com/3.html',
        error: function()
        {
           alert('file does not exists');
        },
        success: function()
        {
            alert('file exists');
        }
    });

Using Javascript

function checkIfRemoteFileExists(fileToCheck)
{
    var tmp=new Image;
    tmp.src=fileToCheck;

    if(tmp.complete)        
        alert(fileToCheck+" is available");        
    else        
     alert(fileToCheck+" is not available");        
}

Now to check if file exists or not call js function like this

checkIfRemoteFileExists('http://www.yoursite.com/abc.html');​