简体   繁体   English

外部javascript文件中的php

[英]php in external javascript file

I have a the code below in my js which obviously works if the js is in the php file 我的js中有以下代码,如果js在php文件中,则该代码显然可以工作

filter: '<?php echo $my_cat>'

how can i make a variable maybe in the php file and pull it in my external js file? 我如何在php文件中创建变量并将其拉到我的外部js文件中?

You have to place this below code at the top part of your test.php page 您必须将此下面的代码放在test.php页面的顶部

<script>
var filter = '<?php echo $my_cat>';
</script>

and simply you can check it on your external.js file by alert 只需通过警报就可以在external.js文件上检查它

alert(filter);
<?php 
   function showme(){
   echo "HelloWorld";
}
?>    

<script type="text/javascript">
     var my_var = '<?php echo showme(); ?>' ;
</script>

You can also use short tags. 您也可以使用短标签。

<script>

var filter = '<?=$my_cat?>';

</script>

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

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