1

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

<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>
4

1 回答 1

1

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

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

});

于 2013-07-20T17:24:49.293 回答