我有 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>
最佳答案
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>
关于javascript - 如何在javascript函数中包含php文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44795861/