如何在javascript函数中包含php文件?

时间:2017-06-28 07:12:08

标签: javascript php

我有2个文件abc.php和xyz.php。当我在移动设备上打开时,它包括abc.php和桌面上的xyz.php。我在下面列出了这些文件,但它不起作用。欢迎任何建议。

<script>
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) {
            <?php include ('abc.php');?>
            } else {
            <?php include ('xyz.php');?>
            }
 </script>

1 个答案:

答案 0 :(得分:1)

PHP是服务器端语言。你不能这样加载。

所以请改用ajax。

<script>
    if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) {
        var include = "abc.php";
    } else {
        var include = "xyz.php";
    }
    $.ajax({
         url:"url to a function where you can include specified file",
         type:"POST",
         data:{file: include},
         success: function(){
              // you can get executed content of a file here. 
         }
    });

</script>