add a jquery script using pure javascript

1

I'm trying to execute the following code from php:

echo '<script type="text/javascript">
    var script = document.createElement("script");
    var executefunction = "'."
        $.jAlert({
            'title': 'Alerta de Mesa de Control ',
            'content': '".$why."',
            'theme': 'green',
            'btns': { 
                'text': 'Cerrar',
                'onClick' : function(){
                    window.WWC(id,name);
                    window.location.reload(true);
                }
            },
            'onClose': function(){
                window.WWC(id,name);
                window.location.reload(true);
            },
        });';
    script.innerHTML = executefunction;
    document.body.appendChild(script);
    </script>".'"';

The main idea was that once the javascript code enters html , take the jquery tag and paste it in body at the end.

but I get an error:

Uncaught SyntaxError: Invalid or unexpected token por el $.jAlert.

Update

Trying to declare it as a function and store it as string :

<script>
                document.addEventListener('DOMContentLoaded', function(event) { 
                    var script = document.createElement('script');
                    var executefunction =  new function (".'"'."$.jAlert({'title': 'Alerta de Mesa de Control ','content': '" . $why . "','theme': 'green','btns': {'text': 'Cerrar','onClick' : function(){window.WWC(id,name);window.location.reload(true);}},'onClose': function(){window.WWC(id,name);window.location.reload(true);},})".'"'.");
                    script.innerHTML = executefunction();
                    document.body.appendChild(script);
                });
            </script>

I stay as follows:

echo "<script type='text/javascript'>
                        $.jAlert({
                                'title': 'Alerta del Sistema',
                                'class': 'usertimer',
                                'content': '".$why."',
                                'theme': 'red',
                                'closeBtn': true
                            });
            </script>";
    
asked by Francisco Núñez 22.12.2017 в 21:19
source

1 answer

0

You have an odd concatenation after declaring var executefunction = **"'."** , which causes you to end up having to do the same and maybe for that, it gives you that error escapes a single quote ' , or those of two " , in a single sentence, you would stay like this:

echo "<script>
      var script = document.createElement('script');
      var executefunction =  $.jAlert({

        'title': 'Alerta de Mesa de Control ',
        'content': '".$why."',
        'theme': 'green',
        'btns': { 
            'text': 'Cerrar',
            onClick : function(){
                window.WWC(id,name);
                window.location.reload(true);
            }
        },
        onClose: function(){
            window.WWC(id,name);
            window.location.reload(true);
        },
    });
    script.innerHTML = executefunction;
    document.body.appendChild(script);
</script>";
    
answered by 22.12.2017 / 21:34
source