我有一個圖片網址(http://t2.gstatic.com/images?q=tbn:ANd9GcTR_RHZrrb25Cx1qGPul6PYsCnVsIqtRuJxRS1Bj0I8DPXqQx-zow)。我想驗證這在JavaScript中是否圖像存在或不。請幫助我找到解決方案。驗證圖片網址
Q
驗證圖片網址
0
A
回答
0
您的文章與之前由anothershrubery發佈的Change image source if file exists幾乎相同。
你可以試試這個
<img src="http://mydomain.com/images/image1.jpg" onerror="this.src='http://mydomain2.com/images/image1.jpg'" />
或者
<script language="JavaScript"><!-
function testImage(URL) {
var tester=new Image();
tester.onLoad=isGood;
tester.onError=isBad;
tester.src=URL;
}
function isGood() {
alert('That image exists!');
}
function isBad() {
alert('That image does no exist!');
}
//--></script>
1
可以在一個隱藏的DIV加載圖像並檢查圖像加載。 我建議你使用jQuery來實現這一點。
負載事件延伸閱讀:http://api.jquery.com/load-event/
HTML:
<div id="image-container" style="display: none">
<img src="http://your/image/url">
</div>
的Javascript/jQuery代碼:
$('img', '#image-container ').load(function() {
// image loaded
});
2
try {
var img = document.createElement("img");
img.src ="http://t2.gstatic.com/images?q=tbn:ANd9GcTR_RHZrrb25Cx1qGPul6PYsCnVsIqtRuJxRS1Bj0I8DPXqQx-zowsd";
} catch(err)
{
//
}
if(img.height > 0) {
//image exists
} else {
// image not exists
}
如果文件存在的[
相關問題
- 1. jQuery驗證 - 網址驗證
- 2. 驗證twitter網址
- 3. 圖片網址
- 4. Jquery驗證以驗證網址
- 5. Javascript:驗證外部網址的圖像
- 6. 驗證互聯網地址
- 7. 如何驗證網址?
- 8. IAP收據驗證網址?
- 9. PHP圖片驗證
- 10. 驗證圖片驗證碼。 jquery PHP&javascript
- 11. 驗證網域中的網址格式
- 12. 比較圖片網址
- 13. 從網址加載圖片
- 14. 給圖片框的網址
- 15. 回形針圖片網址
- 16. 更換圖片的網址
- 17. htaccess重寫圖片網址
- 18. 隨機圖片網址
- 19. 圖片的友好網址
- 20. Facebook相冊圖片網址
- 21. 圖片驗證碼問題
- 22. MVC中的圖片驗證
- 23. 隱藏WordPress網站和圖片網址
- 24. 網址驗證 - 接受沒有協議的網址
- 25. Restlet中的HTTP身份驗證,驗證子網址
- 26. 驗證沒有www的網址。或http://
- 27. 驗證網址與JavaScript和PHP
- 28. 如何驗證網址狀態
- 29. 如何使用JS驗證YouTube網址
- 30. asp.net的MVC dataannotation驗證網址
可能重複我該如何檢查在jQuery或Javascript?](http://stackoverflow.com/questions/3646914/how-do-i-check-if-file-exists-in-jquery-or-javascr IPT) – Curt 2012-03-06 10:05:43