希望我理解正确,您正在尝试检查页面上是否加载了图像。如果不是,您将用户重定向到谷歌让说。如果图像加载正确,为什么要重定向用户?
在此处提供更多详细信息。
无论如何,这将执行检查页面是否可访问(jquery + ajax)并在您成功加载图像或图像加载失败时重定向用户。
$('#your_img_id')
.load(function(){
$.ajax('http://www.rgagnon.com'), {
statusCode: {
404: function() {
alert("not working");
window.location='http://www.google.com'
},
200: function() {
alert("working");
window.location='http://www.rgagnon.com';
}
}
});
})
.error(function(){
window.location='http://www.google.com';
});
更新:
如果 href 在本地,则为页面上的所有锚点添加带有辅助页面的新属性。
<body>
<a href="http://www.rgagnon.com/page-1.html" fallbackhref="http://www.google.com/page-1" />
<a href="http://www.rgagnon.com/page-2.html" fallbackhref="http://www.google.com/page-2" />
<a href="http://www.rgagnon.com/page-3.html" fallbackhref="http://www.google.com/page-3" />
</body>
如果初始 href 不可用,现在用后备值覆盖 href
$("a").each(function() {
//exit if hreffallback undefiend
if ($(this).attr('fallbackhref')== undefined) return;
$.ajax($(this).attr('href')), {
statusCode: {
404: function() {
alert("not working");
$(this).attr('href', $(this).attr('fallbackhref'));
},
200: function() {
alert($(this).attr('href') + " accesible");
//do notihng
}
}
});
});
如果您想以其他方式重定向,那是不可能的。如果您将从 otside www.rgagnon.com 访问您的本地网站,则根本无法访问,则不会执行重定向。
如果您在实时网站上遇到此问题,请重写您的所有应用并使用没有域的相对路径,而不是 href="www.rgagnon.com/your-page/" 使用 href="/your-page/"