使用 PHP 动态创建 Javascript 的更简单方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6584438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 21:11:51  来源:igfitidea点击:

Easier way to dynamically create Javascript using PHP

phpjavascriptjquerymysqlhtml

提问by Nyxynyxx

I'm currently using PHP to dynamically create a javascript which will be echoedon the page.

我目前正在使用 PHP 动态创建将echoed在页面上的javascript 。

Sample Code:

示例代码:

$JS .= '<script>';

if($condition == true) {
    $JS .= 'alert("Yo its true omg!");
}

$JS .= "</script>";

As you can see, this will eventually get messy with ll the 'quotes and escaping of single quotes within the double quotes...

如您所见,这最终会因所有'引号和双引号内的单引号转义而变得混乱......

Is there a better way to do this?

有一个更好的方法吗?

回答by Hubro

You want the heredocsyntax!

你想要heredoc语法!

if($condition)
    $statement = <<<JS
        alert("Wohoo!");
JS;
else $statement = "";

$javascript = <<<JS
    <script>
        $statement
    </script>
JS;

To handle conditional statements inside heredoc strings, simply do the condition logic beforehand and insert empty or filled strings inside the heredoc string. You can insert variables into heredoc strings the same way you do normal strings.

要处理heredoc 字符串中的条件语句,只需预先执行条件逻辑并在heredoc 字符串中插入空字符串或填充字符串。您可以像插入普通字符串一样将变量插入到 heredoc 字符串中。

If you think heredoc strings are a hassle to define, I agree with you. Unfortunately, as far as I know, it's the only way to escape the even greater quote escaping hassle (No pun intended).

如果你认为 heredoc 字符串定义起来很麻烦,我同意你的看法。不幸的是,据我所知,这是逃避更大的报价逃避麻烦的唯一方法(无意双关语)。

回答by Alex

You would be better to create the dynamic javascript file still as a separate file with the PHP extension then just before outputting the javascript set the header content type to text/javascript

您最好还是将动态 javascript 文件创建为带有 PHP 扩展名的单独文件,然后在输出 javascript 之前将标题内容类型设置为 text/javascript

$js = <<<JS
// Your JS here
JS;

header("Content-type: text/javascript");
echo $js;
exit();

回答by Marco Panichi

If the static javscript code amount is more than your dinamically created javascript code amount, you can simply write javascript code as usual and then inject php where needed.

如果静态 javscript 代码量超过您动态创建的 javascript 代码量,您可以像往常一样简单地编写 javascript 代码,然后在需要的地方注入 php。

Example of a hypothetical index.php:

假设的 index.php 示例:

<?php

    $message = "Yo its true omg!";

?>

<html><head>

    <script>
        alert("<?php echo $message; ?>");
    </script>

</head><body>

    ...

</body></html>

回答by g.d.d.c

Create a PHP file, but use a content header to make it 'look' like a JS file to the Browser. Because the file extension is .php Apache (or whatever other web server you're using) will still parse it, but the browser will see it as a JS file and run the contents properly. I use this frequently for CSS files because it allows me to declare variables and take advantage of loops and other more powerful control concepts lacking from CSS.

创建一个 PHP 文件,但使用内容标题使其在浏览器中“看起来”像一个 JS 文件。因为文件扩展名是 .php,Apache(或您使用的任何其他 Web 服务器)仍会解析它,但浏览器会将其视为 JS 文件并正确运行内容。我经常将它用于 CSS 文件,因为它允许我声明变量并利用循环和其他更强大的 CSS 缺乏的控制概念。

回答by Manu

echo <<<EOF
 <script>
      alert("Yo it's true omg!");
 </script>

EOF;

回答by Taitu-lism

split your javascript into files:

将您的 javascript 拆分为文件:

file name: 'javascript1.js' contains:

文件名:'javascript1.js' 包含:

alert(1);

file name: 'javascript2.js' contains:

文件名:'javascript2.js' 包含:

alert(2);

file name: 'javascript3.js' contains:

文件名:'javascript3.js' 包含:

alert(3);

make sure they can be joined correctly.

确保它们可以正确连接。

then the php:

然后是php:

$final_output = file_get_contents('javascript1.js');

if ($condition) {
    $final_output .= file_get_contents('javascript2.js');
}

$final_output .= file_get_contents('javascript3.js');

echo $final_output;