当嵌入式链接不存在时,如何输出警报消息

How to output an alert message when embedded link does not exist?

本文关键字:输出 消息 何输出 链接 嵌入式 不存在      更新时间:2023-09-26

我有这段代码,我想知道当嵌入的链接不存在时,如何显示警报消息。这可能吗?

<script type="text/javascript">
    function soniaZsound(track) {
    var link = 'https://ssl.gstatic.com/dictionary/static/sounds/de/0/'+track+'.mp3';
    document.getElementById("myspan").innerHTML='<embed src="'+link+'"' +
      'onError="alert("sorry this word is not in the database");"' + 
      '`autostart=true loop=false hidden=true` type="audio/mp3">';
    }
</script> 
<body>    
  <span id="myspan"></span>
  <form name= "searchwords" id="searchwords"> 
    How does this word sound: <input type="text" name= "soundSearch"><input type="button"
    value="GO" onclick="soniaZsound(document.forms['searchwords']['soundSearch'].value);">
  </form>
</body>

如果允许您从该域读取内容,您可以执行HTTP HEAD请求以查看文件是否存在:

$.ajax({
url:'http://www.example.com/somefile.ext',
type:'HEAD',
error:
    function(){
        //file does not exists
    },
success:
    function(){
        //file exists
    }

});