0

I need to include some javascript in my php file but im having difficulty with the quotes inside the javascript

This is the javascript:

<scripttype="text/javascript">

    var sc_project = $$$$;

    var sc_invisible = $;

    var sc_security = "$$$$$$$";

    var scJsHost = (("https:" == document.location.protocol) ? "https://secure." : "http://www.")

    document.write("<sc" + "ript type='text/javascript' src='" + scJsHost + "statcounter.com/counter/counter.js'></" + "script>");

</script>

I tried

echo {
    '<scripttype="text/javascript">

                var sc_project = $$$$$$;

                var sc_invisible = $;

                var sc_security = "$$$$$$";

                var scJsHost = (("https:" == document.location.protocol) ? "https://secure." : "http://www.")

                document.write("<sc" + "ript' . 'type=\'text/javascript\' src='' . ' + scJsHost + "statcounter.com/counter/counter.js'.'></" + "script>");

            </script>';
        }

Any way to have this appear on my php page?

4

4 に答える 4

0

For one thing... can't you just close your PHP and then repoen it?

If not, try escaping the quotes. I generally like to wrap everything in double quotes, so I can put my variables inline without concatenation.

Also why are you using . concatentation in the javascript. I don't see any php there to concatenate.

于 2013-02-06T23:12:50.297 に答える
0

Your script tag should be:

<script type="text/javascript">

Because type is an attribute.

When you are using quotes in PHP, you can mix quotes for simple things...

$test = '<p id="example">Example using mixed quotes</p>';

And you can escape quotes if it gets more complicated...

$test = '<p id="example">Example using \'escaped\' quotes</p>';

So you could use this:

echo '<script type="text/javascript">

    var sc_project = $$$$;

    var sc_invisible = $;

    var sc_security = "$$$$$$$";

    var scJsHost = (("https:" == document.location.protocol) ? "https://secure." : "http://www.")

    document.write("<sc" + "ript type=\'text/javascript\' src=\'" + scJsHost + "statcounter.com/counter/counter.js\'></" + "script>");

</script>';
于 2013-02-06T23:14:13.947 に答える
0

It's php, you don't need echo. Just use

...
?>
<script>
  var sc_project = $$$$$$;
  ...
</script>
<?php
...

Since your script is outputting the page anyway.

于 2013-02-06T23:15:08.080 に答える
0

Use nowdoc:

$js = <<<'JS'
<scripttype="text/javascript">

    var sc_project = $$$$;

    var sc_invisible = $;

    var sc_security = "$$$$$$$";

    var scJsHost = (("https:" == document.location.protocol) ? "https://secure." : "http://www.")

    document.write("<sc" + "ript type='text/javascript' src='" + scJsHost + "statcounter.com/counter/counter.js'></" + "script>");

</script>
JS;

echo $js;
于 2013-02-06T23:16:26.710 に答える