I am very new to this topic so it might be a simple question but I could not answer it by googling.
I have two HTML files and a javascript file (see below). The first HTML file contains an input field where an user can enter a temperature in degree celcius. This number is then converted to the temperature in Kelvin using a javascript function called "convTemp".
Problem is the output. I would like to get the result printed on both HTML pages (in "< p id="output" > text to be replaced < /p >") but currently it works only for one of the files. How can I modify the javascript file/HTML files to update both HTML files at the same time and not only one? Any comments are welcome! If it is not possible using javascript, how else could it be done?
Here are the files I use demonstrating the problem using a small toy example:
HTML file number 1:
<html>
<head>
<title>JavaScript-Test</title>
<script src="JSfunc.js" type="text/javascript"></script>
</head>
<body>
<form name="Calc">
<input type="text" name="myNumber" size="3">
<input type="button" value="convert temperatures" onclick="convTemp(document.Calc.myNumber.value, 'output')"><BR>
<p id="output">Here the output will appear and a new tab will open as well.</p>
</body>
</html>
HTML file number 2:
<html>
<head>
<title>HTML number 2</title>
<script src="JSfunc.js" type="text/javascript"></script>
</head>
<body>
<p id="output">This replacement does not work yet</p>
</body>
</html>
Javascript file:
function convTemp(x, tID) {
var res = parseFloat(x) + 273.15;
window.open('htmlfile2.html'); /*, "_self" */
document.getElementById(tID).innerHTML = x + " degrees are " + res + " Kelvin.";
}
Edit: I tried the localStorage solution but it still does not work. The js file now looks as follows:
function convTemp(x) {
var res = parseFloat(x) + 273.15;
/*window.open('htmlfile2.html'); /*, "_self" */
if (typeof(Storage) != "undefined") {
localStorage.setItem("resCalc", res);
window.location.href = 'htmlfile2.html';
document.getElementById("output").innerHTML = x + " degrees are " + localStorage.getItem("resCalc") + " Kelvin.";
}
else {
document.getElementById("output").innerHTML = "Sorry, your browser does not support Web Storage...";
}
}
Any ideas? Thanks!
Edit 2: I added a working solution (see below). If anyone has a better solution, please post it - thanks!
By using localStorage - as suggested by epascarello and Michael - I could solve the problem although I doubt that it is the most aesthetic one.
Here are the files, maybe someone else runs into this problem or can suggest a better solution later on:
htmlFile1.html:
htmlfile.2.html:
And the js file JSfunc.js:
Comments/improvements are always welcome!
I think you need
postMessage
to do this. You can send a message to other window so that you can update both.Maybe:
And in HTML file number 2:
Send it in the querystring
read it on the next page
You can only alter the current DOM with javascript, but you can use e.g. cookies to get the data displayed on both pages after changing those on one page. Preferably, if you use php or any server side language too, the way to go would be to save the entries in your database and pull them from there.
here's a function taken from this answer to create and retrieve cookies.
USAGE:
Set the cookie:
JSFunc.js:
You can then load the content from the cookie on page load:
JSFunc2.js (let's say the cookie is called 'entry'):