javascript - Could a person parse a value from a php page to an external java script page?

one text

Solution:

Your echoed script has to be executed AFTER the element with the id user has been parsed. You have multiple ways to achieve this.

  1. Echo your script anywhere after you element with the id user. - At best right before the </body> (common practice).

  2. Let JS tell you, when the DOM has been loaded (place this JS whereever you like):

    window.addEventListener ('load', function () {
        setName("<?= $phpVariable ?>"); 
    });
    

Example:

<script>
    console.log('Test script-tag before');
    console.log(document.getElementById('selectMe'));
    
    window.addEventListener ('load', function () {
      console.log('Listen for load-event');
      console.log(document.getElementById('selectMe'));
    });
</script>

<div id='selectMe'>Select Me</div>

<script>
    console.log('Test script-tag after');
    console.log(document.getElementById('selectMe'));
</script>

Source