Forum Moderators: open

Message Too Old, No Replies

Stop script on 301 response

         

jon22

8:11 pm on Oct 28, 2013 (gmt 0)

10+ Year Member



I'm implementing jQuery infinite scroll (http://www.infinite-scroll.com) into my product search results but it keeps loading the last results over and over. I've found that the after the last page the php script I'm using redirects you with a 301 to another page instead of a 404 so the infinite scroll script has no way of knowing it's on the last page.
Is there a way I could check the response first then kill the script if it's 301? Or is there another way to solve this?

Here's my I/S script...

<script type="text/javascript">
$(function(){

var $container = $('#catProdContainer');

$container.imagesLoaded(function(){
$container.masonry({
itemSelector: '.productlisting-box',
columnWidth: 113,
bufferPx: 0
});
});


// Infinite Scroll
$container.infinitescroll({
navSelector : '.paginationNav', // selector for the paged navigation
nextSelector : '.paginationNav a.button-productlisting-next:last', // selector for the NEXT link (to page 2)
itemSelector : '.productlisting-box', // selector for all items you'll retrieve
loading: {
finishedMsg: '<div class="thumb-scroll"><i class="icon-thumbs-up"></i></div>',
//img: '/images/ajax-loader.gif',
pixelsFromNavToBottom: 40
},
debug : true
},

// trigger Masonry as a callback
function( newElements ) {
// hide new items while they are loading
var $newElems = $( newElements ).css({ opacity: 0 });
// ensure that images load before adding to masonry layout
$newElems.imagesLoaded(function(){
// show elems now they're ready
$newElems.animate({ opacity: 1 });
$container.masonry( 'appended', $newElems, true );
});
});
});

</script>


My Firebug console log, as you can see after the last page a 301 to /bestselling/ :

GET http://example.com/surf/4/

200 OK
201ms
jquery.min.js (line 4)
["contentSelector", div#catProdContainer.masonry]
jquery....min.js (line 1)
["math:", 1059, 627.5999755859375]
jquery....min.js (line 1)
["math:", 834, 627.5999755859375]
jquery....min.js (line 1)
["math:", 668, 627.5999755859375]
jquery....min.js (line 1)
["math:", 326, 627.5999755859375]
jquery....min.js (line 1)
["heading into ajax", "http://example.com/surf/5/"]
jquery....min.js (line 1)
Using HTML via .load() method
jquery....min.js (line 1)
GET http://example.com/surf/5/

301 Moved Permanently
134ms
jquery.min.js (line 4)
GET http://example.com/surf/4/bestselling/8/

200 OK
204ms

Any help very much appreciated

[edited by: whoisgregg at 4:02 pm (utc) on Nov 19, 2013]
[edit reason] exemplified links, pls see sticky mail [/edit]

lucy24

8:52 pm on Oct 28, 2013 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I've found that the after the last page the php script I'm using redirects you with a 301 to another page instead of a 404 so the infinite scroll script has no way of knowing it's on the last page.
Is there a way I could check the response first then kill the script if it's 301? Or is there another way to solve this?

Surely you'd want to fix the php script to give the correct response? Otherwise you're just applying a bandaid.

The fact that the php issues a non-200 response means it's recognizing in some way that it's dealing with bum input. Is it structured as "if you're given a variable outside the range x-y, redirect to something within the range, otherwise increment"? If so you may need a special case for the variable y+1.

I suspect that the real problem is in the way the php and javascript interact. One or the other has to be able to say "the buck stops here".