欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php动态生成JavaScript代码

程序员文章站 2022-06-15 23:22:47
复制代码 代码如下:

复制代码 代码如下:

<?php
echo <<<js //使用多行输出的方法输出javascript代码
<script language = "javascript">
function func()
{
if(confirm("are you ok with this?"))
{
document.write("i'm ok!");
}
else
{
document.write("i'm not ok!");
}
}
</script>
js;
?>
<html>
<head>
</head>
<body>
<a href="#" href="#" onclick="javascript:func();">please click</a>
</body>
</html>

复制代码 代码如下:

<?php
$confirm = "are you ok with this?"; //定义确认框上的信息
$ok_msg = "i'm ok!"; //ok时的信息
$not_ok_msg = "i'm not ok!"; //cancel时的信息

echo "<script language = \"javascript\">"; //开始逐行输出javascript代码
echo "function func()";
echo "{";
echo "if(confirm(\"are you ok with this?\"))";
echo "{";
echo "document.write(\"i'm ok!\");";
echo "}";
echo "else";
echo "{";
echo "document.write(\"i'm not ok!\");";
echo "}";
echo "}";
echo "</script>"; //输出结束
?>
<html>
<head>
</head>
<body>
<a href="#" href="#" onclick="javascript:func();">please click</a>
</body>
</html>