1

I have a counter button which increases the number when clicked.
Also I have an URL like http://localhost:5000/x/.

What I want is:
Printing the counter value at the end of the URL.

For example:
If counter=3,
I want to go http://localhost:5000/x/3;

My button type is submit and it increases the value of counter and goes the pagehttp://localhost:5000/x/counter. However, I got 404 Error because the data is on the http://localhost:5000/x/3. Here is the code.

<form action="http://localhost:5000/x/counter">

<script>
var add = (function () {
    var counter = 0;
    return function () {return counter += 1;}
})();

function myFunction(){
    document.getElementById("demo").innerHTML = add();
}
</script>

Thanks.Additional I don't use any framework and I want to handle it just by using JavaScript.

EDIT: Thanks for your answers. I solved it by using window.location.href function.

Anıl Selvi
  • 121
  • 1
  • 1
  • 11
  • 1
    Possible duplicate of [How to set form action through JavaScript?](http://stackoverflow.com/questions/2701041/how-to-set-form-action-through-javascript) – XCS May 17 '17 at 18:59
  • You could have a look at this thread: https://stackoverflow.com/questions/1090948/change-url-parameters – Steven May 17 '17 at 18:59
  • which server are you using? – sbharti May 17 '17 at 19:14

1 Answers1

0

Retrieve counter using url of page

var counter = location.href.substring(location.href.lastIndexOf('/')+1);
counter = parseInt(counter);
counter += 1;

Set myform action using counter

document.myform.action = location.href.substring(0,location.href.lastIndexOf('/'))+counter;

Name your form

<form name='myform'>
sbharti
  • 899
  • 11
  • 22