Javascript 如何在php代码中添加脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3556381/
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
how to add script inside a php code?
提问by champ
How can I add script inside a php code? suppose i want to give an alert for a button click.. how can i do that??
如何在 php 代码中添加脚本?假设我想为单击按钮发出警报.. 我该怎么做?
回答by Pat
You can just echo all the HTML as normal:
您可以正常回显所有 HTML:
<?php
echo '<input type="button" onclick="alert(\'Clicky!\')"/>';
?>
回答by Haim Evgi
<?php
echo"<script language='javascript'>
</script>
";
?>
回答by Jeff
You mean JavaScript? Just output it like anything else in the page:
你是说 JavaScript?只需像页面中的其他任何内容一样输出它:
<script type="text/javascript">
<?php echo "alert('message');"; ?>
</script>
If want PHP to generate a custom message for the alert dialog, then basically you want to write your JavaScript as usual in the HTML, but insert PHP echo statements in the middle of your JavaScript where you want the messages, like:
如果想让 PHP 为警报对话框生成自定义消息,那么基本上您想像往常一样在 HTML 中编写 JavaScript,但是在您想要消息的 JavaScript 中间插入 PHP echo 语句,例如:
<script type="text/javascript">
alert('<?php echo $custom_message; ?>');
</script>
Or you could even do something like this:
或者你甚至可以做这样的事情:
<script type="text/javascript">
var alertMsg = '<?php echo $custom_message; ?>';
alert(alertMsg);
</script>
Basically, think about where in your JavaScript you want PHP to generate dynamic output and just put an echo statement there.
基本上,考虑一下您希望 PHP 在 JavaScript 中的哪个位置生成动态输出,然后在那里放置一个 echo 语句。
回答by Piotr Müller
To avoid escaping lot of characters:
为避免转义大量字符:
echo <<<MYSCRIPT
... script here...
MYSCRIPT;
or just turn off php parsing for a while:
或者只是关闭 php 解析一段时间:
?>
...your script here
<?php
回答by joronimo
You could use PHP's file_get_contents();
你可以使用 PHP 的 file_get_contents();
<?php
$script = file_get_contents('javascriptFile.js');
echo "<script>".$script."</script>";
?>
For more information on the function:
有关该功能的更多信息:
回答by Dai
You mean you want to show a javascript alert when a button is clicked on a PHP generated page?
您的意思是您想在 PHP 生成的页面上单击按钮时显示 javascript 警报?
echo('<button type="button" onclick="alert(\'Alrt Text!\');">My Button</button>');
Would do that
会这样做
回答by Your Common Sense
Exactly the same way you add HTML tags. Echo it
与添加 HTML 标签的方式完全相同。呼应它
回答by jiwopene
You can insert script to HTML like in any other (non-PHP) page, PHP processes it like any other code:
您可以像在任何其他(非 PHP)页面中一样将脚本插入 HTML,PHP 会像处理任何其他代码一样处理它:
<button id="butt">
→ Click ME! ←
</button>
<script>
document.getElementById("butt").onclick = function () {
alert("Message");
}
</script>
You can use onSOMETHINGattributes:
您可以使用onSOMETHING属性:
<button onclick="alert('Message')">Button</button>
To generate message in PHP, use json_encodefunction (it can convert to JavaScript everything that can be expressed in JSON — arrays, objects, strings, …):
要在 PHP 中生成消息,请使用json_encode函数(它可以将所有可以用 JSON 表达的东西——数组、对象、字符串……)转换为 JavaScript:
<?php $message = "Your message variable"; ?>
<button onclick="alert(<?=htmlspecialchars(json_encode($message), ENT_QUOTES)?>)">Click me!</button>
If you generate code for <script>tags, do NOTuse htmlspecialcharsor similar function:
如果您为<script>标签生成代码,请勿使用htmlspecialchars或类似功能:
<?php $var = "Test string"; ?>
<button id="butt">Button</button>
<script>
document.getElementById("butt").onclick = function () {
alert(<?=json_encode($var)?>);
}
</script>
You can generate whole JavaScript files, not only JavaScript embedded into HTML. You still have to name them with .phpextension (like script.php). Just send the correct header.
您可以生成整个 JavaScript 文件,而不仅仅是嵌入到 HTML 中的 JavaScript。您仍然必须使用.php扩展名(如script.php)命名它们。只需发送正确的标题。
script.php– The JavaScript file
script.php– JavaScript 文件
<?php header("Content-Type: application/javascript"); /* This meant the file can be used in script tag */ ?>
<?php $var = "Message"; ?>
document.getElementById("butt").onclick = function () {
alert(<?=json_encode($var)?>);
}
index.html– Example page that uses script.php
index.html– 使用的示例页面 script.php
<!doctype html>
<html lang=en>
<head>
<meta charset="utf-8">
<title>Page title</title>
</head>
<body>
<button id="butt">
BUTTON
</button>
<script src="script.js"></script>
</body>
</html>
回答by Raman kuamr
One way to avoid accidentally including the same script twice is to implement a script management module in your templating system. The typical way to include a script is to use the SCRIPT tag in your HTML page.
避免意外包含相同脚本两次的一种方法是在模板系统中实现脚本管理模块。包含脚本的典型方法是在 HTML 页面中使用 SCRIPT 标记。
<script type="text/javascript" src="menu_1.0.17.js"></script>
An alternative in PHP would be to create a function called insertScript.
PHP 中的另一种方法是创建一个名为 insertScript 的函数。
<?php insertScript("menu.js") ?>

