简体   繁体   English

替换Java代码中的代码( <script></script> )通过PHP

[英]Replace code in Javascript (<script></script>) by PHP

for example, i have a javascript code like this, 例如,我有这样的JavaScript代码,

<script type="text/javascript">
    function test(){
        alert('hi');
    }
</script>

can we replace that code in PHP to : 我们可以将PHP中的代码替换为:

<script type="text/javascript">
    function example(){
        alert('hohoho');
    }
</script>

that i want to replace is text in the tag script. 我要替换的是标记脚本中的文本。

is it possible? 可能吗?

Declare a variable to store the text to be alerted and then use it. 声明一个变量以存储要警告的文本​​,然后使用它。

<?php
    $variable = "hohoho";// change this to what you want in alert.
?>
<script type="text/javascript">
    function example(){
        alert('<?php echo $variable ?>');
    }
</script>

For obfuscating see this , this and this 对于obfuscating请参阅thisthisthis

It is possible to put all JavaScript tags inside php but remember php get executed once at server side when page get called by client but JavaScript will executed on client side call (browser side). 可以将所有JavaScript标记放入php中,但请记住,当客户端调用页面时,php在服务器端执行一次,但是JavaScript将在客户端调用(浏览器端)执行。

So you can do some thing like this and it will work, just to answer your question but it is not a good practice, you should have reason to use it: 因此,您可以执行以下操作,并且它会起作用,只是回答您的问题,但这不是一个好习惯,您应该有理由使用它:

<?php
$content = '<script type="text/javascript">
    function example(){
        alert("hohoho");
    }
</script>';

if (true)
    echo $content;
?>


<script>example();</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM