简体   繁体   中英

add a permenant html element with javascript

i want to make so when I create the TD element for the HTML I want it to stay there after the page refreshes. Right now the element just disappears when I refresh the page. Can someone give me some Tips on how to solve this problem?

 if(SkottX == posX && SkottY >= 510){ alert("Your Finale Score is " + Score); myFunction(); location.reload(); } function myFunction() { var table = document.getElementById("table"); var row = table.insertRow(0); var cell1 = row.insertCell(0); var cell2 = row.insertCell(1); cell1.innerHTML = "Score:"; cell2.innerHTML = Score; } 
 <html> <head> <meta charset="UTF-8"> <style> table, td { border: 1px solid black; } td{ height: 20px; width: 25px; } </style> </head> <body onLoad="Initialise()"> <canvas id="myCanvas" width="900" height="600" style="border:10px solid #000070"> </canvas> <button onclick="gameState = states.RUNNING">Start!</button> <table id="table" height="900" width="1200" style="display: inline-block"> </table> <script src="JSgameStarship3.js"> </script> </body> </html> 

If you only modify the in-memory DOM of the current page, then that is all you will modify.

To persist the changes, you need to explicitly store them.

There are two basic approaches to this:

  1. Store them somewhere on the client (such as in localStorage ) and use JavaScript to look for the data you stored with the page loads and to reapply the changes if any are found.
  2. Make an HTTP request (eg with XMLHttpRequest ) to the server and use server-side code to store the change. If you take this approach, you'll probably want to generate the page using server-side and and data from a database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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