-1

My current page link is https://www.somesite.com/Vijesti.aspx?vijestID=2122 How I can read url parameter after "?" and make link +1 or -1 on that page? I need to create link that after click goes on page parameter ?vijestID=2123 for +1 or ?vijestID=2121 for -1. Thanks for help

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
Superbart
  • 1
  • 1

2 Answers2

0

If you're looking to create "next" and "previous" functionality, you can use location.search to get the query string, and parse through it to get your value.

if( location.search.indexOf( "vijestID=" ) !== -1 )
    currentId = parseInt(location.search.match(/vijestID=(\d+)/)[1]);

This will have the ID simply in an integer form, and you can add or subtract 1 from it to setup the link you need for your setup.

Sunny Patel
  • 7,830
  • 2
  • 31
  • 46
0

You could place this script wherever you want the <a> tags to go, then modify it as you see fit:

<script>
  (function() {
    var vijestID = parse_query('vijestID');

    var prev = document.createElement('a');
    prev.href = 'https://www.somesite.com/Vijesti.aspx?vijestID=' + (vijestID - 1);
    prev.innerHTML = '<';
    document.body.appendChild(prev);

    var next = document.createElement('a');
    next.href = 'https://www.somesite.com/Vijesti.aspx?vijestID=' + (vijestID + 1);
    next.innerHTML = '>';
    document.body.appendChild(next);

    function parse_query(arg){
        var url = location.href;
        var qs = url.substring(url.indexOf('?') + 1).split('&');
        for(var i = 0, result = {}; i < qs.length; i++){
            qs[i] = qs[i].split('=');
            result[qs[i][0]] = decodeURIComponent(qs[i][1]);
        }
        return arg ? result[arg] : result;
    }
  })();
</script>
Paul
  • 139,544
  • 27
  • 275
  • 264