view var Javascript in div html

Asked

Viewed 2,817 times

2

The code

<section class="content">
    <span></span>
    <span></span>
    <script type="text/javascript" src="../js/purl.js"></script>
    <script>
        var vars = geturlvar()['Nome'];//traz embalagem.html se colocar um alert!
    </script>

    <img src="../images/aguarde.gif" id="image">
    <div id="url" w3-include-html=vars></div>       

</section>

I need to take the value that is in vars (which is packing.html) and put inside the <div id="url" w3-include-html=vars></div>

how do I do that?

  • use: Document.getElementById("url"). innerHTML = vars;

  • I put this code under var vars = geturlvar()['Name'] inside the <script> tag, but the following error appears when debugging: Uncaught Typeerror: Cannot set Property 'innerHTML' of null, but if I put an Alert or in the console I get the value of the vars variable ok.

  • You’re wearing it before the div is created. Place your javascript code after creating the div id="url" or create a function scope in javascript q waits for page loading to load the javascript later

  • but if I create javascript after div it error in vars variable that was not set, you could make an example, sorry I’m new in javascript

  • Below are two examples that solve your problem. One given by @Artur and the other by me!

3 answers

3

You are trying to put the value in the div before it is created. Just put your code inside window.onload, for it to run after the screen is loaded

window.onload = function () {
   var vars = "url_de_teste";
   document.getElementById('url').innerHTML = vars;
}
<div id="url"></div>

1

Try it like this:

<section class="content">
    <span></span>
    <span></span>
    <script type="text/javascript" src="../js/purl.js"></script>


    <img src="../images/aguarde.gif" id="image">
    <div id="url" w3-include-html=vars></div>     

    <script>
        var vars = geturlvar()['Nome'];
        document.getElementById("url").innerHTML = vars;
    </script>  

</section>
  • it returns Undefined and the link that was supposed to be http://localhost/take/pages/package.html, is http://localhost/take/pages/vars

  • Your geturlvar()['Name'] is bringing some value?

  • had forgotten to pass the parameters in the URL, he brought, however, just printed on the screen the text embalagem.html, how do I make it include W3-include-html, because it does include the.html packaging page inside the.html container.

  • just remembering that W3.includeHTML' and a javascript that does include

  • Your W3-include-html=vars does include than inside the div id="url"?

  • that if I put it like this it works: <div id="url" W3-include-html="packages.html"></div>

Show 1 more comment

1


Thank you all for your help, but I decided to change the method of including the.html packaging page into a.html container by:

<script type="text/javascript" src="../js/purl.js"></script>
<script>
    var vars = geturlvar()['Nome'];
    $(function () {
        $("#includedContent").load(vars);
    });
</script> 

and in the body was:

 <div id="includedContent"></div>

Browser other questions tagged

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