检测的JavaScript在PHP,在PHP构建页面 [英] Detecting javascript in PHP, before PHP builds page

查看:121
本文介绍了检测的JavaScript在PHP,在PHP构建页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现对面,描述了如何使用PHP和检测JavaScript的网页几个职位的我已经把我的自己的解决方案一起的(下同),但...

I have found several posts across the web which describe how to detect javascript using PHP and I have already put together my own solution together (below) but...

...我想知道如果我能提高我有什么了。

...I am wondering if I can improve on what I have already.

摘要

1)我想尽可能快加载移动优化页面越好。

1) I want to load a mobile-optimised page as quickly as possible.

2)的.js功能的浏览器我要加载的(不容易获得,但更快的加载整个页面)的配置B 的 - 与以后(的意图,即在的onload )的页面的重新配置部分 - 因之引发的各种JavaScript事件的用户 - 如(更方便)的配置A

2) For .js-enabled browsers I want to load the entire page in (less accessible but faster loading) Configuration B - with the intention of later (ie. after onload) reconfiguring parts of the page - consequent to the user triggering various javascript events - as (more accessible) Configuration A.

3)对于非.js文件功能的浏览器,我想直接加载整个页面(更方便)的配置A 的。

3) For non-.js-enabled browsers, I want to load the entire page directly in (more accessible) Configuration A.

解决方案(到目前为止)

下面的.js <脚本> 是尽可能高的< HEAD> 我敢把它(仅低于<冠军> )。

The following .js <script> is as high up in the <head> as I dared place it (just below the <title>).

<?php
echo '

<script>
if (window.location.href != \'http://'.$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'].'?enhance=js\') {
        window.location.href = \'http://'.$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'].'?enhance=js\';}
</script>';

$hasJS = 0; if (in_array('js',$_GET)) {$hasJS++;}
?>

正如你所看到的,&LT;脚本&GT;功能的浏览器来检查特定讲述的.js GET 参数附加到URI(加强= JS ),如果该参数不存在,要使用 GET 参数追加到URI。

As you can see, the <script> tells .js enabled browsers to check for a specific GET parameter appended to the URI (enhance=js) and if the parameter is absent, to reload the page immediately with that GET parameter appended to the URI.

PHP则检查GET参数存在,如果是这样,一个标志, $ hasJS ,开启告诉PHP脚本的其余部分,该浏览器是.js文件功能。

PHP then checks if the GET parameter exists and if it does, a flag, $hasJS, is switched on to tell the rest of the PHP script that the browser is .js-enabled.

到目前为止,一切都很好。它的工作原理。

So far, so good. It works.

不过,我可以改善这种通过PHP方法.js文件检测?

But can I improve on this .js detection via PHP method?

问题:

1)是否有办法做到这一点不涉及改变URI或重新加载页面?

1) Is there a way to do it which doesn't involve changing the URI or reloading the page?

2)除了URI,还有没有其他的页面相关的属性,我可以检索与PHP相同的数据之前添加使用JavaScript的数据?

2) Apart from the URI, is there any other page-related attribute to which I can append data with javascript before retrieving that same data with PHP?

推荐答案

浏览器不告诉服务器的​​JavaScript是否启用。

The browser does not tell the server whether or not JavaScript is enabled.

最好的解决方法是发送的配置A 的大家谁访问,然后JavaScript的网页变成的配置B 的是这样的:

The best solution is to send Configuration A to everyone who visits, and then javascript changes the page to Configuration B like this:

<script type="text/javascript">
  if (document.cookie.match('js=1') == null) {
    document.cookie = 'js=1; path=/'
    document.location = document.location
  }
</script>

然后在你的PHP:

Then in your PHP:

if ($_COOKIE['js']) {
  // send Configuration B
}

通过上面的code时,JScookie将被当用户关闭浏览器中删除。您也可以发送一个到期日期,使其持续更长的时间,更多的细节在这里: http://www.quirksmode.org/js /cookies.html

With the above code, the "js" cookie will be deleted when the user closes the browser. You can also send an expiry date to make it last longer, more details here: http://www.quirksmode.org/js/cookies.html

这篇关于检测的JavaScript在PHP,在PHP构建页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆