我正在使用一个脚本,通过解析来自" blogurl.com/page /#"的帖子来自动加载博客帖子。页面设置为的URL。
我正在使用测试博客,目前我有2页测试帖。
当我向下滚动到某一点时,第2页的帖子会加载并解析到容器。我在第3页上没有任何帖子,因此第3页不存在(即blogurl.com/page/3不是真正的网址)。但是,此脚本仅检查网址上是否有帖子,而不是网址本身是否存在。
(function($) {
$.fn.swoosh = function(loadingImgUrl, callback) {
if (!loadingImgUrl) {
loadingImgUrl = "Loading...";
}
if (callback == null) {
callback = -1;
}
var postList = this;
var turnOff = false;
var pageNumber = 2;
var urlArray = window.location.href.toString().split("/");
var blogUrl = urlArray[0] + "//" + urlArray[2] + "/" + urlArray[3] + "/";
var baseUrl = blogUrl + "page/";
var postUrl = "";
var processing = false;
//insert the loading bar at the end of the posts-list
if (loadingImgUrl != "Loading...") {
postList.parent().append('<div class="loading"><img src="' + loadingImgUrl + '"></div>');
} else {
postList.parent().append('<div class="loading">' + loadingImgUrl + '</div>');
}
$(".loading").hide(); //make sure loading bar is hidden
$(document).scroll(function() {
//kick out of function if it's already running, if it has been shut off, or if there is no 2nd page
if (processing || turnOff || pageNumber == 0) {
return false;
}
//when scrolling gets to the footer, start chugging!
if ($(window).scrollTop() >= $(document).height() - $(window).height() - $(".blog_footer").height() - 150) {
processing = true;
//currently processessing, so don't call function again until done
$(".loading").fadeIn(200); //fade in loading bar
postUrl = baseUrl + pageNumber; //calculate the page to load
//AJAX CALL
$.get(postUrl, function(data) {
//grab only post items from the loaded page
var posts = $(data).find(".col-item");
//check that the loaded page has content
if (posts.length > 0) {
console.log(loadingImgUrl);
//fade out the loading bar, then attach the new items to the end of the list
$(".loading").fadeOut(200, function() {
posts.appendTo(".blog-listing .container-wrap");
});
pageNumber++; //increment the page to load.
$(".next-posts-link").attr("href", baseUrl + pageNumber);
}
//if the loaded page doesn't have content, it means we have reached the end.
else {
turnOff = true;
$(".next-posts-link").after('<div class="next-posts-link unactive">Next</div>');
$(".next-posts-link:not(.unactive)").remove();
$(".loading").fadeOut(200);
}
processing = false; //we are done processing, so set up for the next time
setTimeout(function() {
twttr.widgets.load();
IN.parse();
FB.XFBML.parse();
gapi.plusone.go();
}, 350);
});
}
});
};
})(jQuery);
&#13;
这是一个非常笨重的脚本。当它试图加载一个不存在的页面时,控制台会在网址上收到404,然后加载......&#34;文本保留在页面底部。我尝试过的几件事情都不起作用。有什么建议吗?
编辑**我认为检查网址是否存在的显而易见的地方是:
pageNumber++; //increment the page to load.
$(".next-posts-link").attr("href", baseUrl + pageNumber);
因为这是pageNumber增加的地方,然后传递回postUrl:
postUrl = baseUrl + pageNumber; //calculate the page to load
//AJAX CALL
$.get(postUrl, function(data) {
虽然不确定我是否正常。
编辑***认为实时链接可能会有所帮助:
答案 0 :(得分:1)
您是否尝试过添加失败处理程序: https://api.jquery.com/jquery.get/
$.get(postUrl, function(data){
...
...
}).fail(function(){
//whatever you need to do here
});
如果请求返回404
,则应触发此操作您也可以使用它来检查请求的状态:
.fail(function(response){
if(response.status == 404){
...
}
}