admin管理员组文章数量:1277317
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
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
Share Improve this question edited Sep 1, 2012 at 9:27 Dan Barzilay 4,9935 gold badges28 silver badges40 bronze badges asked Sep 1, 2012 at 9:02 Muhammad Faiq BakhtyarMuhammad Faiq Bakhtyar 311 gold badge1 silver badge6 bronze badges 1- 2 you could request the sites with ajax and check if the status code is not 200 – GottZ Commented Sep 1, 2012 at 9:06
3 Answers
Reset to default 6You can use ajax for check file exists or not
Using Jquery
$.ajax({
url:'http://www.example./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.plete)
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./abc.html');
i like to use this type of script
function CheckFileExist(fileToCheck: string) {
return new Promise((resolve, reject) => {
fetch(fileToCheck).then(res => {
if (res.status == 404) resolve(false);
if (res.status == 200) resolve(true);
return res.text()
})
})
}
and use it
var exists = await CheckFileExist(link);
- There is an issue with @Sibu's solution: it actually downloads the file (it can be potentionally big, wasting traffic)
- In the 2021, one should not use jQuery in new projects
- native Promises and Fetch are the way to go today
<output id="output"></output>
<script>
// create a non-cached HTTP HEAD request
const fileExists = file =>
fetch(file, {method: 'HEAD', cache: 'no-store'})
.then(r => r.status==200);
// check the file existence on the server
// and place the link asynchronously after the response is given
const placeNext = file => fileExists(file).then(yes => output.innerHTML =
(yes ? `<a href="3.html">Next</a>` : '')
);
// place the "next" link in the output if "3.html" exists on the server
placeNext('3.html');
</script>
本文标签: Check html file exist on server using javascriptStack Overflow
版权声明:本文标题:Check html file exist on server using javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741241113a2363997.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论