para club101 para club101 - 1 year ago 89
HTML Question

how to transfer data from one html page to another using javascript

I'm trying to transfer data from one html page to another page, so I wrote this program:


<a href="/events" onclick="send()">
<span class="message">
new alert

function send() {
$mesg = $('.message');
var msg = $mesg.text();
var msg1 = msg.replace(/\s/g, '');
localStorage.setItem("message", msg1);


<script src=""></script>
document.getElementById("event").innerHTML = localStorage.getItem("message");
<td id="event"></td>

But I don't get the data transferred from the pages? what is my mistake?

Answer Source

The issue is in page2.ejs:

Problem: The HTMLElement <td id="event"></td> is not yet available when your <script> block is parsed and executed, so document.getElementById("event") returns undefined.

Solution: Move the <script> block to the bottom, beneath <td id="event"></td> and it should work.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download