Next a...">

Append the last segment of the current url on button click

I have

a btn

<button id="next" onclick="inlastSegment()">Next</button>

and url

http://localhost:8888/projects/oop/2


My goal

when i clicked on this btn i want to increase it.

Example

  • http://localhost:8888/projects/oop/3

  • http://localhost:8888/projects/oop/4

  • http://localhost:8888/projects/oop/5

I tried

<script type="text/javascript">

var href = location.href; // http://localhost:8888/projects/oop/2
var lastSegment = window.location.pathname.split('/').pop();
console.log(lastSegment); //2

function inlastSegment() {
    lastSegment = (parseInt(lastSegment)+1);
    console.log(lastSegment); //3
    //Stuck
}

</script>

      


I'm a bit stuck, any hints / suggestions would be much appreciated.

Is it possible to do this in PHP? Just out of curiosity.

+3


source to share


1 answer


use location.replace

like this:



<script type="text/javascript">
    var pathArr = window.location.pathname.split('/'), lastSegment = pathArr.pop();
    function inlastSegment() {
        pathArr.push(++lastSegment);
        location.replace(pathArr.join('/'));
    }

</script>

      

+4


source







All Articles