JS for conditional pagination