i have the following jquery for onclick code :
function find(){
$.ajax({
type: "post",
url: link 'register/data',
data: {},
success: function () {
window.location.href= "mycontroller/myfunction"
}
});
}
i want to make the url stays on mycontroller/myfunction everytime they click the button, i tried substring lastindexof but it keeps adding more url everytime i click the button, how do i make the button stays on that whenever i click it? thanks in advance
CodePudding user response:
Let's pretend you're on http://example.com
. You redirect the page to "test"
, so now you're on http://example.com/test
. Now you redirect to "test"
again. Uh oh, now you're on http://example.com/test/test
. Didn't want that to happen, did you? You redirect it just one more time to "other/test"
. Oh no, now you're on http://example.com/test/test/other/test
!
The way to solve this resides in a single bytes. Change test
to /test
, so it'll always compare to the base URL instead of the current one.
In your JavaScript:
window.location.href = "/mycontroller/myfunction";
However you click it you'll always be on http://example.com/mycontroller/myfunction
.
CodePudding user response:
You can change the success function to use window.location.origin
like this:
function find(){
$.ajax({
type: "post",
url: link 'register/data',
data: {},
success: function () {
window.location.href = window.location.origin "/mycontroller/myfunction"
}
});
}