简体   繁体   English

根据屏幕宽度大小包括PHP

[英]Including PHP depending on screen width size

I read a lot about it, I tried a lot as well... I almost think it's possible, but I need your help. 我读了很多关于它的内容,我也做了很多尝试……我几乎认为这是可能的,但是我需要您的帮助。

I want to load some content depending on the screen width. 我想根据屏幕宽度加载一些内容。 It's a sticky navbar with social network sharing buttons (follow on twitter, like on facebook, the usual). 这是一个带有社交网络共享按钮的粘性导航栏(通常在Twitter上,例如在facebook上)。

Using this code, It loads the html, perfectly. 使用此代码,可以完美地加载html。 BUT the scripts inside it, does not. 但是里面没有脚本。 I use jQuery animation to show the buttons when the cursor is over the logo, and the buttons use javascript to, well, work. 当光标在徽标上方时,我使用jQuery动画显示按钮,并且按钮使用javascript正常工作。 But, loading this way, it's like there was no scripts inside that html. 但是,以这种方式加载,就好像该html中没有脚本。

$(function () {
   var width = $(window).width();
   if (document.documentElement.clientWidth >= 992) 
      $('#Navbar').load('navbar.html');
});

I tried with PHP, then. 然后,我尝试使用PHP。 A simple include() worked. 一个简单的include()起作用了。 The navbar was loaded the way it was supposed to. 导航栏已按预期方式加载。

Since PHP cannot know the screen width (and since I am using bootstrap, I following it's screen sizes), I thought I could do this (I changed the .html to .php, it's all the same) 由于PHP无法知道屏幕宽度(并且由于我使用的是引导程序,因此我会遵循它的屏幕尺寸),所以我认为我可以做到这一点(将.html更改为.php,都一样)

$('#NavBar').append('<?php include "navbar.php"; ?>');

But it doesn't work. 但这是行不通的。

Is there a way for me, using jQuery, to call the include() ? 有没有一种使用jQuery的方法来调用include() Or any other way that could make this work? 或任何其他方式可以使这项工作?

Media queries and display: hidden are not options. 媒体查询和display: hidden不是选项。 I don't want to load the content and keep it hidden. 我不想加载内容并保持隐藏状态。 I want to just load it when it's needed. 我只想在需要时加载它。

These are two completely different programming languages so you can't call a php function from your javascript. 这是两种完全不同的编程语言,因此您无法从JavaScript调用php函数。

but you can call a reload of the page with some parameters or some kind of new url to make this work with php. 但您可以使用某些参数或某种新的网址调用页面的重新加载,以使其与php一起使用。

Example onepage php site: 单页php网站示例:

<html>
<head>
<!--Your Stuff-->
</head>
<body>
<?php
  //access the parameter and it's value with php's global $_GET
  if(isset($_GET["mobile"]) && $_GET["mobile"] == 992){
    include "navbar.php";
  }
?>
<script>
//check if the document has the specified width and also if the parameter is already set
if (document.documentElement.clientWidth >= 992 && window.location.href.indexOf("?mobile=992") == -1) {
  window.location.href = window.location.href + "?mobile=992";
}
</script>
</body>
</html>

The Example above works if you don't have any additional get parameters on the site and navbar.php only contains valid html stuff. 如果您在网站上没有任何其他get参数,并且navbar.php仅包含有效的html内容,则上面的示例适用。

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

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