Why is there a problem with the transfer of data through the global variable js? When I go to the page where I use the value of a variable (second.html), the following error occurs:
Uncaught TypeError: Cannot read property 'globalVar' of undefined
fi.js
function sent(){ window.storage = {}; window.storage.globalVar = document.getElementById('input').value; } se.js
var d = document; var name = window.storage.globalVar; function addRow() { var tbody = d.getElementById('table').getElementsByTagName('TBODY')[0]; var row = d.createElement("TR"); tbody.appendChild(row); var td1 = d.createElement("TD"); row.appendChild(td1); td1.innerHTML = name; } first.html
<html lang="ru"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <script src="se.js"></script> </head> <body> <form action="" method="post" onsubmit="sent();"> <input type="text" id="input"> <input type="submit" value="ΠΡΠΏΡΠ°Π²ΠΈΡΡ"> </form> </body> </html> second.html
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <script src="fi.js"></script> </head> <body onload="addRow();return false;"> <table id="table"> <thead> <tr> <th>Some</th> </tr> </thead> <tbody> </tbody> </table> </body>
globalVarbecomes global (and in general will be announced) you need to executesent()- Misha Saidovsent()should be executed beforevar name = window.storage.globalVar. Ifsent()is executed beforeaddRow(), then perhaps the solution for you will be to place these 2 linesvar d = document; var name = window.storage.globalVar;var d = document; var name = window.storage.globalVar;in addRow () (and performaddRow()aftersent()once again) - Misha Saidov