如何从PHP调用JavaScript函数?

<?php

  jsfunction();
  // or
  echo(jsfunction());
  // or
  // Anything else?

下面的代码来自xyz.html(单击按钮),它调用外部xyz.js中的wait()。这个wait()调用wait.php。

function wait() 
{
  xmlhttp=GetXmlHttpObject();
  var url="wait.php"; \
  xmlhttp.onreadystatechange=statechanged; 
  xmlhttp.open("GET", url, true); 
  xmlhttp.send(null);
} 

function statechanged()
{ 
  if(xmlhttp.readyState==4) {
       document.getElementById("txt").innerHTML=xmlhttp.responseText;
  }
}

和wait.php

<?php echo "<script> loadxml(); </script>"; 

其中loadxml()以同样的方式从另一个PHP文件调用代码。

除此之外,loadxml()工作得很好,但它没有以我想要的方式调用。


当前回答

我不接受反对者的答案。

如果你发现了一些特殊的软件包可以让它工作,那么你可以自己做!所以,我不相信这些答案。

onClick是一个涉及最终用户的拼凑,因此是不可接受的。

@umesh很接近,但它不是一个独立的程序。以下是这样的(摘自他的回答):

<script type="text/javascript">
function JSFunction() {
    alert('In test Function');  // This demonstrates that the function was called
}
</script>

<?php
// Call a JS function "from" php

if (true) {   // This if() is to point out that you might
              // want to call JSFunction conditionally
    // An echo like this is how you implant the 'call' in a way
    // that it will be invoked in the client.
    echo '<script type="text/javascript">
         JSFunction();
    </script>';
}

函数在使用之前声明是很重要的。(我不知道“以前”是指“词汇上的以前”还是“时间上的以前”;在上面的示例代码中,两者都是。)

其他回答

我不接受反对者的答案。

如果你发现了一些特殊的软件包可以让它工作,那么你可以自己做!所以,我不相信这些答案。

onClick是一个涉及最终用户的拼凑,因此是不可接受的。

@umesh很接近,但它不是一个独立的程序。以下是这样的(摘自他的回答):

<script type="text/javascript">
function JSFunction() {
    alert('In test Function');  // This demonstrates that the function was called
}
</script>

<?php
// Call a JS function "from" php

if (true) {   // This if() is to point out that you might
              // want to call JSFunction conditionally
    // An echo like this is how you implant the 'call' in a way
    // that it will be invoked in the client.
    echo '<script type="text/javascript">
         JSFunction();
    </script>';
}

函数在使用之前声明是很重要的。(我不知道“以前”是指“词汇上的以前”还是“时间上的以前”;在上面的示例代码中,两者都是。)

现在(2012年2月)有一个新功能。检查在这里

代码示例(取自web):

<?php

$v8 = new V8Js();

/* basic.js */
$JS = <<< EOT
len = print('Hello' + ' ' + 'World!' + "\\n");
len;
EOT;

try {
  var_dump($v8->executeString($JS, 'basic.js'));
} catch (V8JsException $e) {
  var_dump($e);
}

?>

对于一些后端节点处理,您可以通过shell运行JS脚本,并通过console.log将结果返回给PHP

  function executeNode($script)
  {
    return shell_exec('node -e \'eval(Buffer.from("'.base64_encode($script).'", "base64").toString())\'');
  }


  $jsCode = 'var a=1; var b=2; console.log(a+b);';
  
  echo executeNode($jsCode);

这不可能。PHP是服务器端语言,JavaScript是客户端语言,它们彼此并不了解。您需要一个服务器端的JavaScript解释器(如Aptanas Jaxer)。也许您真正想做的是使用Ajax,如Architecture (JavaScript函数异步调用PHP脚本并对结果进行处理)。

<td onClick= loadxml()><i>Click for Details</i></td>

function loadxml()
{
    result = loadScriptWithAjax("/script.php?event=button_clicked");
    alert(result);
}

// script.php
<?php
    if($_GET['event'] == 'button_clicked')
        echo "\"You clicked a button\"";
?>

你也可以试试这个:-

    public function PHPFunction()
    {
            echo '<script type="text/javascript">
                 test();
            </script>'; 
    }
    <script type="text/javascript">
    public function test()
    {
        alert('In test Function');
    }
    </script>