Is there a way to add a CSRF token to all instances where window.location.href
is used in my Javascript code?
It's not possible to override the window.location object and its properties like
window.location.href
.Creating a universal function to include the CSRF token in all instances would be time-consuming.
addCSRFAndProceed(url);
function addCSRFAndProceed (url) {
window.location.href = url + getCSRFTokenAndValue();
}
Backend code Here is how the CSRF cookie is set from the backend.
$_COOKIE['CSRFTOKEN'] = '123456';
HTML code
<div class="redirect-button" onclick="TriggerRequest()">
Javascript code
function TriggerRequest() {
window.location.href="www.mysite.com/?index.php&from=desktop";
}
I use various functions that utilize window.location.href
and am seeking a generic solution for adding the CSRF token to the URL instead of manually updating each function separately.