繁体   English   中英

如何在index.html中添加一个php文件

[英]How to add a php file in index.html

我想显示图像。
在“ Windows Surface RT”上,它必须为580px。
在所有其他设备中,它必须为413px。

我正在使用以下代码来检测操作系统:

var OSName="Unknown OS";
if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";
if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";

但是我无法在jquery中检测到设备类型。 所以我用了这个php代码。

//Get Device Info
$mobile = new Mobile_Detect;
$deviceType = ($mobile->isMobile() ? ($mobile->isTablet() ? 'tablet' : 'phone') : 'computer');
$scriptVersion = $mobile->getScriptVersion();

我不想将index.html更改为index.php。
那么,有没有办法拥有一个单独的php文件,并将其添加到index.html head标签中?

您可以包括一个php文件,就好像它是一个javascript文件一样,并在其中设置值。

在您的HTML文件中...

<script type="text/javascript" src="device-detect.php"></script>

在device-detect.php ...

//Get Device Info
$mobile = new Mobile_Detect;
$deviceType = ($mobile->isMobile() ? ($mobile->isTablet() ? 'tablet' : 'phone') : 'computer');
$scriptVersion = $mobile->getScriptVersion();

echo "var scriptVersion = '$scriptVersion';";

之后包含或运行的所有javscript都可以访问变量scriptVersion

您不能在html file include php file ,但是可以使用$ .ajax()将 load php datahtml page ,例如,

$(function(){
    $.ajax({
        type: "POST",
        url: 'page.php',
        success: function(data){
            $(data).appendTo('body');// data is html
        }
    });
});

暂无
暂无

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

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