简体   繁体   中英

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

filter: '<?php echo $my_cat>'

how can i make a variable maybe in the php file and pull it in my external js file?

You have to place this below code at the top part of your test.php page

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

and simply you can check it on your external.js file by alert

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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