检测javascript不工作时损坏的图像
我已经尝试了许多使用javascript检测损坏图像的解决方案,但似乎都不起作用。知道我做错了什么吗 从此处的下拉框中选择“jquery”以查看损坏的图像:- 该网站的代码如下:-检测javascript不工作时损坏的图像,javascript,jquery,image,Javascript,Jquery,Image,我已经尝试了许多使用javascript检测损坏图像的解决方案,但似乎都不起作用。知道我做错了什么吗 从此处的下拉框中选择“jquery”以查看损坏的图像:- 该网站的代码如下:- <!doctype html> <html> <head> <meta charset=utf-8> <title>Insights Lab</title> <link href='http://fonts.goog
<!doctype html>
<html>
<head>
<meta charset=utf-8>
<title>Insights Lab</title>
<link href='http://fonts.googleapis.com/css?family=Roboto+Condensed' rel='stylesheet' type='text/css'>
<style>
ul li { color: red;}
.emphasis { color: green;}
.code {color: #3387B5}
body { font-family: 'Roboto Condensed', sans-serif; }
p { font-size: 14px; color:#999; }
h2 { font-size: 18px;
color:#666;
}
.view {
bottom: 10;
height: 16px;
position: absolute;
}
#art {
position: relative;
width:220px;
border: 1px solid #999;
float:left;
margin-right:10px;
padding:5px;
height:300px;
margin-bottom:8px;
background-color: #EEEEEE;
}
</style>
<script language="JavaScript" type="text/javascript">
function onImgErrorSmall(source)
{
source.src = "http://www.forbes.com/favicon.ico";
// disable onerror to prevent endless loop
source.onerror = "";
return true;
}
function onImgErrorLarge(source)
{
source.src = "http://www.forbes.com/favicon.ico";
// disable onerror to prevent endless loop
source.onerror = "";
return true;
}
</script>
</head>
<body>
<?php
function baseurl($url) {
// you can add more to it if you want
$urlMap = array('com', 'co.uk','it','org','net','ca','io','me','edu','ac.uk','info','au');
$host = "";
//$url = "http://www.google.co.uk";
$urlData = parse_url($url);
$hostData = explode('.', $urlData['host']);
$hostData = array_reverse($hostData);
if(array_search($hostData[1] . '.' . $hostData[0], $urlMap) !== FALSE) {
$host = $hostData[2] . '.' . $hostData[1] . '.' . $hostData[0];
} elseif(array_search($hostData[0], $urlMap) !== FALSE) {
$host = $hostData[1] . '.' . $hostData[0];
}
return $host;
}
function highlight($text, $words) {
preg_match_all('~\w+~', $words, $m);
if(!$m)
return $text;
$re = '~\\b(' . implode('|', $m[0]) . ')\\b~i';
return preg_replace($re, '<span class="code">$0</span>', $text);
}
$q=$_GET["q"];
$con = mysql_connect('mysql.site.com','un','pw');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("getpocket", $con);
$sql="SELECT * FROM `articles` WHERE `given_title` like '%".$q."%' or `excerpt` like '%".$q."%' order by `time_added` desc";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
$tit = highlight($row['given_title'], $q);
$exc = highlight($row['excerpt'], $q);
$url = $row['resolved_url'];
?>
<div id = "art">
<h2><?php echo $tit;?></h2>
<p><?php echo $exc;?></p>
<div class = "view">
<a href ="<?php echo $url;?>" target='_blank'><img src = "http://<?php echo baseurl($url);?>/favicon.ico" height="16" width="16" onerror="onImgErrorSmall(this)"></a>
</div>
</div>
<?php
}
mysql_close($con);
?>
</body>
</html>
知道我做错了什么吗
谢谢
Jonathan在前面加上这个
您可以使用API检测损坏的图像
jQuery('img').error(function() {
//Do stuff if broken image found
});
本文还将帮助您:
您只需发布相关代码,并定义什么是无效的。您对相关代码有什么期望?你得到了什么?显示时是否有任何错误?是否删除现有代码?此外,函数OnimErrorLarge是否保留在头部,var imgs代码是否保留在之前?OnimErrorLarge可以在任何地方,如果这对您有效,您可以使用它,我放弃我的代码权限..我手动检查了它,它工作了。。我添加了OnimErrorLarge函数,然后执行了循环。。它工作了,不管怎样,我做了另一个更新OK-我看到它现在在这里工作,但是我需要它在查询数据库的页面上工作-代码应该在view.html或getarticles.php中吗?whoo-hoo有效-除了弹出警报,我如何将src设置为其他内容?它是-函数onimgerlargesrc{var img=new Image;img.src=src;img.onerror=function{alert'img not found';src=;};}
jQuery('img').error(function() {
//Do stuff if broken image found
});