【问题标题】:How to add a php file in index.html如何在 index.html 中添加 php 文件
【发布时间】:2013-09-13 13:11:22
【问题描述】:

我想显示一张图片。
在“Windows Surface RT”上,它必须是 580 像素。
在所有其他设备中,它必须是 413 像素。

我正在使用这段代码来检测操作系统:

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 头标签中添加它?

【问题讨论】:

  • 假设您有正当理由不想简单地重命名文件 .php,实际上可以在 html 文件中运行 PHP,但您必须修改 htacess 文件,否则页面将被发送直接进入浏览器,无需解析 PHP。只需将以下内容添加到您的 htacess 或根据需要进行编辑... AddType application/x-httpd-php .html

标签: php jquery html tablet


【解决方案1】:

您可以像 javascript 文件一样包含一个 php 文件,并在其中设置值。

在您的 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

【讨论】:

    【解决方案2】:

    你不能在html fileinclude php file,但你可以load php data 到你的 html page 使用 $.ajax() 喜欢,

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

    【讨论】:

      猜你喜欢
      • 2013-06-29
      • 2016-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-07
      • 2021-12-16
      • 2018-11-16
      • 2020-08-23
      相关资源
      最近更新 更多