JS检查链接是否有效


<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Documenttitle>
    <script src="./js/jquery-3.5.1.js">script>
head>
<body>
    <span>视频链接:span>
    <input type="text" id="vidveoUrl" style="width:300px">
    <button id="btn">testbutton>
    <br>

    <span>字幕文件链接span>
    <input type="text" id="srtUrl" style="width:300px">
    <button id="srtbtn">testbutton>
    <script type="text/javascript">

    $("#btn").click(function(){
        var $vidveoUrl = $("#vidveoUrl").val();
        console.log($vidveoUrl);
        var video = document.createElement('video');
        video.onload = function() {
            alert('success, it exsist');
        }
        video.onerror = function() {
            alert('error, couldn\'t load');
        }
        video.src = $vidveoUrl;
        //不同浏览器情况不同,这里判断在该浏览器是否可以播放
        video.oncanplaythrough = function() {
            alert("This file can be played in the current browser");
        };
    })

    $("#srtbtn").click(function(){
        var $srtUrl = $("#srtUrl").val();
        console.log($srtUrl);
        if($srtUrl.trim()==""){
            return;
        }
        $.ajax({
            url: $srtUrl,
            type: 'GET',
            cache:false,
            dataType: "jsonp", 
            complete: function(response) {
                console.log(response)
                if(response.status == 200) {
                    alert('有效');
                } else {
                    alert('无效');
                }
            }
        });
    })
    
    script>
body>
html>

你可能感兴趣的:(js)