如何在php中编写JavaScript代码

时间:2017-12-20 20:27:47

标签: javascript php

你好我想把这个代码写到php文件中 我将在我的网站上传这个文件,我需要它适合我 我想在1个文件php中合并2代码javascript 和thx到alll

<script>
        (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
                (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
                m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
                })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

                ga('create', 'UA-60737367-1', 'auto');
                ga('send', 'pageview');
            </script>


    <script>
                    var total = "";
                    for( var i = 0; i < 1000; i++ ) {
                        total = total + i.toString();
                        history.pushState(0,0, total );
                    }
                </script>

1 个答案:

答案 0 :(得分:0)

使用heredoc语法

<?php
$scripts = <<<SCRIPT
        <script>
            (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
            m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
            })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

            ga('create', 'UA-60737367-1', 'auto');
            ga('send', 'pageview');
        </script>


            <script>
                var total = "";
                for( var i = 0; i < 1000; i++ ) {
                    total = total + i.toString();
                    history.pushState(0,0, total );
                }
            </script>
SCRIPT;
?>

因此,您可以使用echo函数

显示它
<?php
  echo $scripts;
?>