Programmatically Send Use To Different Endpoint
Say the user is on http://example.com/dashboard, How would I programmatically change the endpoint without affecting the root url, ie. on an event the user would be sent to http://e
Solution 1:
use window.location
.You can build your url with help of window location api object
function redirect(endpoint){
var paths = window.location;
var url = `${paths.origin}/${endpoint}`
return url
//for page redirect use window.location.href = url
}
console.log(redirect('one'))
console.log(redirect('two'))
Solution 2:
You can use location.assign()
to load another URL:
$('#elem').on('click', () => location.assign('/some/path'));
This will redirect you to /some/path
on $('#elem')
click (assuming you use jQuery, but that doesn't matter really)
Solution 3:
- Get the root URL using window.location.origin
- Concat that string with whatever endpoint you want to add
- Assign that to window.location.href inside your click event listener callback function
So the code will look something like this:
const BASE_URL = window.location.origin
document.querySelector('#yourButtonId').addEventListener('click', (e) => {
e.preventDefault();
window.location.href = BASE_URL + '/your_end_point';
}
Refer: Window.location addEventListener
Post a Comment for "Programmatically Send Use To Different Endpoint"