Home > front end >  Get value from javascript dict in odoo template's script tag
Get value from javascript dict in odoo template's script tag

Time:11-09

I need to get the value of elt.innerHTML and pass it to fromPage[j].textContent.

Function borrowed from GitHub.

Currently vars['hiddenNumber'] is returning undefined.

elt.style.visibility = "hidden"; works as intended tho.

I believe this might be related with variable scopes, but can't seem to find a way to create a global variable. Tried to update window.value, but got an error that window is not defined.

Using odoo 13, wkhtmltopdf 0.12.5.

<script>
    function subst() {
        var vars = {};
        var x = document.location.search.substring(1).split('&amp;');
        for (var i in x) {
            var z = x[i].split('=', 2);
            vars[z[0]] = unescape(z[1]);
        }
        var index = vars['webpage'].split('.', 4)[3]

         var operations = {
                    // other operations
             'hidden_page_number': function (elt) {
                 elt.style.visibility = "hidden";
                 vars['hiddenNumber'] = elt.innerHTML;  // This value
             },
         };

         for (var klass in operations) {
             var y = document.getElementsByClassName(klass);
             for (var j=0; j&lt;y.length;   j)
             operations[klass](y[j]);
         }

         var fromPage = document.getElementsByClassName('page');
         for(var j = 0; j&lt;fromPage.length; j  )
         fromPage[j].textContent = vars['hiddenNumber'];  // Required here

         var toPage = document.getElementsByClassName('topage');
         for(var j = 0; j&lt;toPage.length; j  )
         toPage[j].textContent = vars.sitepages;
 }
</script>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

just a suggestion: you define vars outside the closure and then define closure where try to update vars but I suppose that inside the closure it creates new scope for vars and that's why vars is not updated outside

  • Related