I try to deduce the value 1 from the <span id="st">1</span> , as soon as I place this code not in the * .php file but in a separate file for * .js scripts, the code does not work. Tell me how to make this code run from another file?

 <html> <body> <span id="st">1</span> <script src="/project/unix.js" defer async> </script> </body> </html> <script> var page = document.getElementById('st'); var fff =page.firstChild.nodeValue; alert(fff); </script> 
  • <script src="путь_к_вашему.js/> - Sublihim
  • <script src="/project/unix.js" defer async> </script in my project so it is written, the rest of the scripts work perfectly. What I don’t know with this code is that it’s not working ( - Eliot
  • remove async :) - Pavel Mayorov

1 answer 1

Perhaps the script is loaded earlier than DOM.

 function ready() { ...... } document.addEventListener("DOMContentLoaded", ready); 

https://learn.javascript.ru/onload-ondomcontentloaded

In general, what error is written in the console?