[英]jQuery / Ajax: How to call function on targeted page after redirect
[英]How to redirect to a page and then execute a jQuery function call
如何重定向到頁面然后執行帶有一些參數的函數。
例如:Page1.js
if (condition is true) {
window.location.href("Index","Page2");
someFunction();
}
有沒有辦法捕獲重定向並執行Page2中的函數
請幫助我,提前致謝
請試試這個javascript:
第 1 頁:
if (true) {
window.open("page2.html?myVar1=42&myVar2=66", '_blank');
}
第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);
第1頁代碼:
if (condition is true) {
window.location.href = '/yourpage2?act=runfunc';
}
第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.