最好的答案是禁用链接,如果它是不活动的,在用户尝试点击它之前(为什么让它们尝试?)。
jQuery(function($) { // make sure dom is ready
$.ajax({
url: url,
//dataType: 'JSONP', //might need this?
complete: function(xhr) { // use complete so it fires on error OR success
if(xhr.status == 200 || xhr.status == 304) {
$('#link').addClass('valid');
}
else {
$('#link').addClass('invalid').click(function() { return false; });
}
}
});
});
但是,如果你不希望调用,因为那里将是成千上万的用户在看每分钟页面...你
jQuery(function($) { // make sure dom is ready
$('#link').click(function() {
$.ajax({
url: url,
//dataType: 'JSONP', //might need this?
success: function() {
window.location = url;
},
error: function() {
// does this work with JSONP? be sure to check!
window.alert('could not connect');
}
}
});
});
你能详细说一下吗? –