使用javascript检查服务器上存在的html文件 [英] Check html file exist on server using javascript
问题描述
我的ASPX代码生成了一些html文件,我只是把链接放在了分页中,比如
< a href =1。 HTML>首先< / A>&安培; NBSP; |&安培; NBSP;
< a href =3.html>下一步< / a>& nbsp; |& nbsp;
< a href =1.html>上一个< / a>& nbsp; |& nbsp;
< a href =9.html>最后< / a>
说明如果当前用户在第二页上时按下Next移动到第3页...
现在的问题是当用户多次单击下一步按钮并且系统正在生成时,让第5页显示错误页面。
有没有什么办法通过javascript检查html文件来检查文件是否存在?
请帮助我从这个显示停止问题中解脱出来
您可以使用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> |
<a href="3.html">Next</a> |
<a href="1.html">Previous</a> |
<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');
这篇关于使用javascript检查服务器上存在的html文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!