I’m trying to pass a variable to window.open() method. I would like to use that passed variable to the opened html file.
**<script>
var value = document.getElementById("oper").value;
const Gobtn = document.getElementById("Go");
Gobtn.addEventListener("click", function (){
window.open("calculate.html", value);
});
</script>**
I’m trying to open "calculate.html" and use the content of the variable named "value" in the "calculate.html" file. Thanks.
4
Answers
You can pass the variable to the URL by including it as a parameter.
You can do this like:
For best practice, you should encode the variable as a URL safe variable, using
encodeURIComponent()
.Then ofcourse in your new window, you can use
getQueryParam(value)
to retrieve the parameter.to passing variables i suggest you to look at this similar question:
javascript : sending custom parameters with window.open() but its not working
The second parameter is a target attribute, look at this documentation:
Window open()
Modify
window.open()
method to include the variable as a query parameter in the URL.Retrieve the variable from the opened HTML file (calculate.html).
Might be this be a useful to you thanks.