简体   繁体   English

如何重定向到页面然后执行 jQuery 函数调用

[英]How to redirect to a page and then execute a jQuery function call

How to redirect to page then execute a function with some parameters.如何重定向到页面然后执行带有一些参数的函数。

For Example: Page1.js例如: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有没有办法捕获重定向并执行Page2中的函数

Kindly help me, Thanks in advance请帮助我,提前致谢

Please, try this javascript:请试试这个javascript:

Page 1:第 1 页:

if (true) {
   window.open("page2.html?myVar1=42&myVar2=66", '_blank');
}

Page 2:第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 :第1页代码:

if (condition is true) {

   window.location.href = '/yourpage2?act=runfunc';

}

Page2 Code:第2页代码:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM