繁体   English   中英

如何检查URL是否存在JS

[英]How to Check URL if file exists JS

我需要以下代码的帮助:

http://jsfiddle.net/geetyn6f/422/

我需要做的是在输入中写入URL

然后,当我按“检查”时,答案将出现在第二个输入中,而不是警报中。

URL: <input type="text" name="name">
<button type="submit" value="Submit">Check</button>
Answer: <input type="text" name="answer">


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

// Now to check if file exists call function
checkIfRemoteFileExists('http://www.yoursite.com/abc.html');

checkIfRemoteFileExists('http://www.google.de');

您的代码中缺少一些片段。 但是,总体思路是每次您要发出请求时都获取URL框的文本。 发出请求后,更新答案框。 这只是获得这些值的一种方法。 JQuery具有更简单的方法。 我将把这项研究留给您。 单击“ Run Code Snippet然后尝试。

 function checkIfRemoteFileExists(){ var answerBox = document.getElementById('answer'); //get the box $.ajax({ url: document.getElementById('name').value, //get the value from the url box error: function() { answerBox.value = "file does not exists"; //update answer box }, success: function() { answerBox.value = "file exists"; //update answer box } }); } //attach function to button document.getElementById("button").addEventListener("click", checkIfRemoteFileExists); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> URL: <input type="text" id="name"> <button type="submit" id="button" value="Submit">Check</button> Answer: <input type="text" id="answer"> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM