Javascript loop through li to div from paginate

Below is the code I'm using. The upper part of $('div.pagination... works fine, I can alert(length) , and this gives me the correct page value in the pagination section. The lower part seems to be inoperative. This is for a scraper that will open every page on the forum. If I exit the loop, it will successfully retreaves the url for the page here.length length -=2 - remove the next / previous li from the total.

 $('div.pagination').each(function() { var length = $(this).find('li').length; length -= 2; }); for (var i = 0; var <= length; i++) { var pageToOpen = 'http://someWebsite.com/index/page:' + i; alert(pageToOpen); page.open(pageToOpen, function (status) { if (status == 'success') { logAuctions(); } }}); } 
+4
source share
1 answer

Define your var length external (up to) .each()

Using the .lentgh method, you can skip the indexes of the real page. Therefore, I would suggest capturing the real href s anchor.

FIDDLE DEMO

 var pages = []; // skipping the "Next" and "Last" get all A ahchors $('div.pagination li').slice(0,-2).find('a').each(function(){ pages.push( $(this).attr('href') ); }); $.each(pages, function(i, v){ $('<div>'+ ("http://someWebsite.com"+v) +'</div>').appendTo('#output'); }); /* WILL RESULT IN: http://someWebsite.com/auctions/index/page:2 http://someWebsite.com/auctions/index/page:3 http://someWebsite.com/auctions/index/page:4 */ 
+2
source

All Articles