How to call a JavaScript function from PHP?

后端 未结 10 2358
误落风尘
误落风尘 2020-11-21 23:03

How to call a JavaScript function from PHP?

The followi

相关标签:
10条回答
  • 2020-11-21 23:41

    Per now (February 2012) there's a new feature for this. Check here

    Code sample (taken from the 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);
    }
    
    ?>
    
    0 讨论(0)
  • 2020-11-21 23:41

    If you want to echo it out for later execution it's ok

    If you want to execute the JS and use the results in PHP use V8JS

    V8Js::registerExtension('say_hi', 'print("hey from extension! "); var said_hi=true;', array(), true);
    $v8 = new V8Js();
    $v8->executeString('print("hello from regular code!")', 'test.php');
    $v8->executeString('if (said_hi) { print(" extension already said hi"); }');
    

    You can refer here for further reference: What are Extensions in php v8js?

    If you want to execute HTML&JS and use the output in PHP http://htmlunit.sourceforge.net/ is your solution

    0 讨论(0)
  • 2020-11-21 23:44

    you can try this one also:-

        public function PHPFunction()
        {
                echo '<script type="text/javascript">
                     test();
                </script>'; 
        }
        <script type="text/javascript">
        public function test()
        {
            alert('In test Function');
        }
        </script>
    
    0 讨论(0)
  • 2020-11-21 23:48

    try like this

    <?php
     if(your condition){
         echo "<script> window.onload = function() {
         yourJavascriptFunction(param1, param2);
     }; </script>";
    ?>
    
    0 讨论(0)
提交回复
热议问题