繁体   English   中英

页面重新加载 function 并在初始化一些功能后

[英]Page Reload function and after init some functions

我正在创建一个 reloadPage function,它需要和 Id,然后重新加载(加载)它。 由于加载页面后我无法使用任何功能,所以我基本上想在里面做一个 init function 。

所以这里是我的 function

function reloadPage(id) {
    $("#"+id).load(window.location + " #"+id);
 }

我想做一个init function,所以我可以调用它。

就像

reloadPage(test, init())

这可能吗? 我将如何处理这样的问题。

有很多方法可以将数据从前一个实例传递到页面,其中之一是WebStorage api

因此,为了在页面加载时运行特定的代码块,您可以使用 sessionStorage 存储您想要的任何标志,然后检查该标志是否存在并运行您想要的 function,它可能不是您想要的,但因为您没有提供一个可重复的示例(我如何提出一个好问题? ),我希望它为您提供如何在您的项目中实现此类功能的线索和方向

例如(将其保存为 html,加载并检查您的控制台):


<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"
    integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
</head>
<body>
<input>
<button>TEST</button>
<script>
$(document).on('click', 'button', function () { 
let func = $("input").val() //give your desired function here
sessionStorage.setItem("function", func); //store whatever you typed in the session storage
location.reload(); //now reload the page
})

window.onload = function () { //this function runs code after a page completely loads
$("input").val("function 1") //just for the example i set an initial value to the input
let funct = sessionStorage.getItem("function"); // get the data you stored previously

if( funct === "function 1" ){  // check for specific flags
console.log("This is function 1")
}

if( funct === "function 2" ){
console.log("This is function 2")
}

};
</script>
</body>
</html>

暂无
暂无

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

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