在脚本中传递php变量

时间:2017-01-10 02:42:08

标签: javascript php

我有以下功能,如何添加我的php变量id和fn而不是escape(window.location.href)?

$mRow = $this->row;
$wRow = $mRow->getInfo(); 
$id= $mRow->id;
$fn = $mRow->first_name;

<script language="javascript">
  function fbshareCurrentPage()
    {window.open("https://www.facebook.com/sharer/sharer.php?u="+escape(window.location.href)+"&t="+document.title, '', 
'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=300,width=600');
     return false; }
</script>

3 个答案:

答案 0 :(得分:1)

假设下面的文件是php文件。

$mRow = $this->row;
$wRow = $mRow->getInfo(); 
$id= $mRow->id;
$fn = $mRow->first_name;

<script language="javascript">
  var id = '$id'; // Now you can use id directly in javascript
  var fn = '$fn'; //// Now you can use fn directly in javascript
  function fbshareCurrentPage()
    {window.open("https://www.facebook.com/sharer/sharer.php?u="+id+"&fn="+fn+"&t="+document.title, '', 
'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=300,width=600');
     return false; }
</script>

您可以为javascript varibales指定值,并按照您希望的方式在脚本中使用它们。

答案 1 :(得分:0)

$mRow = $this->row;
$wRow = $mRow->getInfo(); 
$id= $mRow->id;
$fn = $mRow->first_name;

<script language="javascript">
  function fbshareCurrentPage()
    {window.open("https://www.facebook.com/sharer/sharer.php?u="+<?php echo $id; ?>+"&fn="+<?php echo $fn; ?>+"&t="+document.title, '', 
'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=300,width=600');
     return false; }
</script>

答案 2 :(得分:0)

<script language="javascript">
 var id= <?php echo json_encode($id); ?>;
 var fn = <?php echo json_encode($fn); ?>;

 function fbshareCurrentPage()
{window.open("https://www.facebook.com/sharer/sharer.php?u="+id+"/"+fn+"&t="+document.title, '', 
'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=300,width=600');
 return false; }

</script>