简体   繁体   中英

Previous page, next page javascript

I tryed to figure out how to go 1 page up and down with simple links but I couldn't find the answer which helped me out. I have got this url: https://pack-simulator.nl/jsondecode.php?page=1 whenever the button next page is clicked, it has to go to ?page=2 and when the previous page is clicked go to ?page=1 again. The script has a maximum of page 695 and minimum of page 1. I used this code,

 var pageNumber = location.search.split("=")[1]; $('a.next').click(function(){ pageNumber++; if (pageNumber > 695) pageNumber = 695; // or set to 1 window.location = ("?page=" + pageNumber); } $('a.prev').click(function(){ pageNumber--; if (pageNumber < 1) pageNumber = 1; // or set to 695 window.location = ("?page=" + pageNumber); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script> <a class="prev" href="#">Load Prev page</a><br> <a class="next" href="#">Load Next page</a> 

I hope somebody can help me out. Thanks!

Check 这个 out. the _page variable holds 'jasondecode.php' and _current variable holds 'jasondecode'. parseInt on _current thus fails.

location.search holds "?page=1". So, if you change

_url to var _url = location.search.split('?')[1].split('=')

and _page to var _page = url[0]

and _current to var _current = parseInt(url[1])

and form the request variable like

var request = location.protocol + '//' + location.hostname + '/' + _href + "?" + _page + '=' + (_current + 1) + '.html';

That should do it.

Not sure what you try to accomplish with all those vars. If you try to load pages into content via ajax my code will not work, but if you want to simply use window.location to make the browser fetch another page you could try something like this:

var pageNumber = location.search.split("=")[1];

$('a.next').click(function(){
    pageNumber++;
    if (pageNumber > 695) pageNumber = 695; // or set to 1
    window.location.search = ("?page=" + pageNumber);
    return false;
});
$('a.prev').click(function(){
    pageNumber--;
    if (pageNumber < 1) pageNumber = 1; // or set to 695
    window.location.search = ("?page=" + pageNumber);
    return false;
});

If you are simply trying to go to a certain page in your browser's history, try this:

<a class="prev" href="#" onclick="prev()">Load Prev page</a><br>
<a class="next" href="#" onclick="next()">Load Next page</a>
<script>
    function prev() {
        window.history.back();
    }

    function next() {
        window.history.forward();
    }
</script>

When a link is clicked, the browser will load either the previous or next page in its saved history.

If you are not planning to call these functions later, simply take out the tag and its contents and replace the prev() and next() with the contents of those functions like this:

<a class="prev" href="#" onclick="window.history.back()">Load Prev page</a><br>
<a class="next" href="#" onclick="window.history.forward()">Load Next page</a>

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM