mootools
One of the little known JavaScript events is the image onError event. This event is triggered when an image 404's out because it doesn't exist. Broken images can make your website look unprofessional and it's important to fix broken images as soon as possible. I've created a MooTools / PHP script that listens for image errors, triggers an AJAX call to a PHP script, and that PHP script sends an email letting me know about the problem.
鲜为人知JavaScript事件之一是图像onError事件。 当图像404由于不存在而被触发时,会触发此事件。 损坏的图像可能会使您的网站看起来不专业,因此尽快修复损坏的图像很重要。 我创建了一个MooTools / PHP脚本,该脚本侦听图像错误,触发对PHP脚本的AJAX调用,并且该PHP脚本发送一封电子邮件,让我知道该问题。
MooTools JavaScript (The MooTools JavaScript)
window.addEvent('domready',function() {
$$('img').addEvent('error',function() {
var notification = new Request({
url: 'ajax-image-error.php',
method: 'post',
data: {
'image': this.get('src'),
'page': window.location.href
}
}).send();
});
});
You'll notice that I listen for the error event. You'll also notice that I only send two parameters: the image path and the page the broken image is living (or dying) on.
您会注意到我在监听错误事件。 您还将注意到,我仅发送两个参数:图像路径和残破的图像赖以生存(或垂死)的页面。
PHP (The PHP)
if(isset($_POST['image']))
{
$to = 'errors@yourdomain.com';
$from = 'automailer@yourdomain.com';
$subject = 'Broken Image';
$content = "The website is signaling a broken image!\n\nBroken Image Path: ".stripslashes($_POST['image'])."\n\nReferenced on Page: ".stripslashes($_POST['page']);
$result = mail($to,$subject,$content,'From: '.$from."\r\n");
die($result);
}
Nothing special within the email -- just the parameters we put into the AJAX call. This bare email will provide us enough information to solve the problem.
电子邮件中没有什么特别的-仅是我们放入AJAX调用中的参数。 这封裸露的电子邮件将为我们提供解决问题的足够信息。
One addition to the MooTools script could be to remove the image from the page so that the user doesn't notice the problem. Have any suggestions? Share them!
MooTools脚本的另一项功能可能是从页面上删除图像,以使用户不会注意到该问题。 有什么建议吗? 分享他们!
mootools