javascript 使用javascript检查服务器上是否存在html文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12226410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check html file exist on server using javascript
提问by Muhammad Faiq Bakhtyar
My ASPX code generated some html files where I just put link for paging like
我的 ASPX 代码生成了一些 html 文件,我只是在其中放置了用于分页的链接,例如
<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 ...
说如果用户在按 Next 时当前在第二页移动到第三页...
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.
现在的问题是,当用户多次单击“下一步”按钮并且系统正在生成假设第 5 页时,它将显示错误页面。
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
有没有办法通过javascript从html检查文件是否存在?请帮我从这个节目塞子问题中退出
回答by Sibu
You can use ajax for check file exists or not
您可以使用ajax检查文件是否存在
Using Jquery
使用jQuery
$.ajax({
url:'http://www.example.com/3.html',
error: function()
{
alert('file does not exists');
},
success: function()
{
alert('file exists');
}
});
Using Javascript
使用 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
现在检查文件是否存在,像这样调用js函数
checkIfRemoteFileExists('http://www.yoursite.com/abc.html');?