I'm acutally creating a reloadPage function, which takes and Id and then reload(load) it. Since after I load the page I couldn't use any functions, so I basically I want to do a init function within.
So here my function
function reloadPage(id) {
$("#" id).load(window.location " #" id);
}
and I want to do a init function, so I can call it.
would be like
reloadPage(test, init())
is this possible? How would I approach a problem like that.
CodePudding user response:
There are many methods to pass data to a page from a previous instance,one of them is WebStorage api
So in order to run a specific block of code when a page loads you could use sessionStorage to store whatever flag you want,and then check if that flag exists and run your desired function,its probably not what you want but since you did not provide a reproducible example ( How do I ask a good question? ),i hope it gives you clues and directions how to implement such functionality to your projects
For example (save this as html,load it and check your console):
<!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>
You could also learn about Url parameters and pass a query string to your reload url with the value you want to pass.
