How to redirect to page then execute a function with some parameters.
For Example: Page1.js
if (condition is true) {
window.location.href("Index","Page2");
someFunction();
}
Is there a way to capture the redirection and execute the function in Page2
Kindly help me, Thanks in advance
Please, try this javascript:
Page 1:
if (true) {
window.open("page2.html?myVar1=42&myVar2=66", '_blank');
}
Page 2:
var urlParams = new URLSearchParams(window.location.search);
var v1 = urlParams.get('myVar1');
var v2 = urlParams.get('myVar2');
function someFunction(myVar1, myVar2){
console.log(myVar1);
console.log(myVar2);
}
someFunction(v1, v2);
Page1 Code :
if (condition is true) {
window.location.href = '/yourpage2?act=runfunc';
}
Page2 Code:
<script>
var qParam = getUrlParameter('act');
if (qParam=='runfunc') {
someFunction();
}
function getUrlParameter(name) {
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
var results = regex.exec(location.search);
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
};
</script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.