在没有任何按钮的情况下调用Javascript函数来在PHP中触发它

时间:2012-06-28 09:46:26

标签: php javascript function

好吧,在我进入编码的另一个阶段之前,为了确保我不会在错误的树上吠叫,请问是否可以在php中调用javascript函数而无需任何按钮点击操作来触发它。我也不考虑onload()或onready(),因为即时通讯将在php中的foreach循环中调用javascript函数..这个概念在某种程度上是这样的:

<script type="text/javascript">
   function callMe(){
      alert('im called!');
   }
</script>

<body>
  <?php
    .....
    foreach($somevar as $var){
     // assuming it will loop 5 times
    callMe();  // well this is the part where im going to call the javascript function
   }
  ?>
</body>

提前谢谢。

** * *** 编辑 < EM> * ****

这是我打算使用的实际代码:

function AddCoordinate( lat, long )                                                                                                                                   
{
  var path = LinePath.getPath(); 
  path.push( new google.maps.LatLng( lat, long ) );
}


<?php    
    foreach($arrayOfPlotPoints as $key => $value){ 
        $longitude = round($value['longitude'],5);
        $latitude = round($value['latitude'],5);
        $snrLevel = $value['snr_level'];
        echo '<script         
            type="text/javascript">AddCoordinate('.$latitude.','.$longitude.')</script>

?>

实际上这是正确的答案..已经有效..所以我也要提供一个答案

2 个答案:

答案 0 :(得分:8)

输出这个:

echo '<script type="text/javascript">callMe()</script>';

答案 1 :(得分:1)

感谢Donatas ..现在这就是我所拥有的:

function AddCoordinate( lat, long )                                                                                                                                   
{
  var path = LinePath.getPath(); 
  path.push( new google.maps.LatLng( lat, long ) );
}


<?php    
    foreach($arrayOfPlotPoints as $key => $value){ 
        $longitude = round($value['longitude'],5);
        $latitude = round($value['latitude'],5);
        $snrLevel = $value['snr_level'];
        echo '<script         
            type="text/javascript">AddCoordinate('.$latitude.','.$longitude.')</script>

?>

它有效:)