Add value to a <script> property

Asked

Viewed 46 times

0

Hello, I have the following code

<html>

//=======Teste1========
<script
  type="text/javascript"
  src="http://modulo.que.sera.carregado.js"
  data-token="">
</script>

//=======Teste2========
<script>
    scriptTeste = function() {

    var dados = JSON.parse(localStorage.dados);
    var node = document.createElement("script");
    var att = document.createAttribute("data-token");
    att.value = dados.token;
    node.setAttributeNode(att);   

    document.getElementById("idBody").prepend(node);
    }
    scriptTeste();
</script>

</html>

The value of data-token will come by user session, and I need to add it to the script. After loading this initial script should be loaded another Script that depends on the first modules already loaded.

I added in the example above the two codes I tested.

The Teste1 worked only when I left the fixed date-token value. The Teste2 did not work because the second script that will be loaded does not expect this first script to finish loading the modules.

  • But the first script will depend on the value of the second token?

  • No, the first script depends on the Session token, and the second script depends on the full load of the module the first loads.

  • Try replacing scriptTeste(); for window.onload = scriptTeste;.

  • Try putting the attribute defer and onload="scriptTeste()" on the tag: <script defer onload="scriptTeste()"...

  • Good morning, that I had tried too. It didn’t work.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.