PHP:如何判断浏览器是否支持PHP中的javascript?

发布于 2024-10-02 09:11:08 字数 1426 浏览 7 评论 0原文

我正在构建一个 AJAX 深层链接网站。

如果用户尝试使用不支持 Javascript 的浏览器或搜索爬虫访问站点,我希望 PHP 加载页面的所有 HTML 代码。基本上 PHP 会返回整个页面。

相反,当用户尝试使用支持 Javascript 的浏览器访问站点时,我希望 PHP 仅返回模板代码,并让 Javascript (AJAX) 处理其余的事情。基本上 PHP 只会加载设计元素并让 Javascript 用内容填充它们。

我研究了 PHP 的 get_browser() 函数,但它似乎不是一个可靠的工具。业界的做法是看浏览器支持Javascript还是使用PHP的搜索爬虫?


背景

为什么我希望网站具有此行为。

由于我希望仅通过加载地址:example.com 来加载主页,这不会向 PHP 发送任何查询,因此 PHP 返回主页的 HTML 代码。然而,当用户尝试加载以下页面时,这会导致问题:example.com#foo。因此,对于此示例,PHP 将返回主页,加载主页后,Javascript (AJAX) 将更改内容,以便显示 #foo 的正确内容。这将使用户看到主页,因此加载时间会变慢并且用户体验不会那么好。但是,如果我的 PHP 脚本可以弄清楚,如果使用支持 Javascript 的浏览器尝试加载页面,它只会返回网站的模板(没有内容),并且 javascript 会使用内容填充该模板应该显示#foo。另一方面,如果Javascript非分离浏览器或爬虫尝试访问页面example.com#foo,则会返回主页。

我正在使用 SWFaddress (http://www.asual.com/swfaddress/) 库进行深度学习-链接。


编辑

谢谢你们。我之前没有想到使用

这就是我决定要做的事情。 PHP 默认情况下会加载诸如 example.comexample.com#foo 之类的页面(这与 PHP 的 example.com 本质上相同)观点,因为根据定义,片段不会发送到服务器)空白(只是视觉模板),其中包含主页内容的 标签。这样,使用 javascript 的用户将看不到主页,AJAX 将根据 #foo 片段填充页面内容。另一方面,搜索爬虫和没有 JavaScript 的用户将看到主页。

再次感谢您。我认为这是非常简单而优雅的解决方案。如果您有任何进一步的建议,请发表评论或其他答案。

I am building an AJAX deep-linked site.

I want PHP to load all the HTML code of the page if the user is trying to access the site with a Javascript non-supported browser or if it is a search crawler. Basically PHP will return the whole page.

On the contrary, when the user is trying to access the site with Javascript supported browser, I want PHP to return only the template code, and let Javascript (AJAX) take care of the rest. Basically PHP will only load design elements and let Javascript populate them with content.

I looked into PHP's get_browser() function, however it seems it is not such a reliable tool. What is the industry's practice see if the browser supports Javascript or it is a search crawler using PHP?


Background:

Why I want the site to have this behavior.

Since I want the home page to load just by loading the address: example.com, which does not send any query to PHP, PHP returns the HTML code of the home page. This however causes issues when the user tries to load the following page: example.com#foo. So, for this example, PHP will return the home page and once the home page is loaded, Javascript (AJAX) will change the content around so that it shows proper content for #foo. This will make the user to see the home page, therefore load time will be slower and user-experience will not be so nice. However if my PHP script can figure out that if the use with Javascript supported browser is trying to load the page, it will only return the template of the web site, which has no content) and the javascript will populate that template with content whatever is supposed to be displayed for #foo. On the other hand, if the Javascript non-separated browser or a crawler will try to access the page example.com#foo, home page will be returned.

I am using SWFaddress (http://www.asual.com/swfaddress/) library for the deep-linking.


Edit

Thank you guys. I did not think of using <noscript></noscript> before.

Here is what I decided to do. PHP by default will load pages such as example.com or example.com#foo (which is essentially the same as example.com from PHP's point of view since fragments by definition are not sent to the server) blank (just visual template) with <noscript> tag inside for the content of the home page. This way users with javascript will not see the home page and AJAX will populate the content of the page according to the #foo fragment. On the other hand, search crawlers and users without javascript will see a home page.

Thank you again. I think this is pretty simple and elegant solution. If you have any further suggestions, please post a comment or another answer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

青衫负雪 2024-10-09 09:11:08

您无法使用 PHP 来执行此操作。你可以做的是使用 noscript 标签重定向到另一个没有 javascript 的 php 页面:

<noscript>
<meta http-equiv="refresh" content="0; URL=nojavascript.php">
</noscript>

You can't do this using PHP. What you can do though is use a noscript tag to redirect to another php page if they don't have javascript:

<noscript>
<meta http-equiv="refresh" content="0; URL=nojavascript.php">
</noscript>
通知家属抬走 2024-10-09 09:11:08

以您尝试的方式来完成此任务是不可能的。

很少有人关闭了 JS 并且不知道。

PHP 在# 之后不会传递任何内容,只有 javascript 可以对此执行任何操作。因此,即使 PHP 可以确定浏览器是否打开了 javascript,它仍然无法读取 # 。

It's not possible to accomplish this in the way you're trying to do it.

It's rare that someone has JS turned off and doesn't know it.

PHP doesn't get passed anything after #, only javascript can do anything with that. So even if PHP could determine if the browser has javascript turned on then it still couldn't read # anyways.

岁月静好 2024-10-09 09:11:08

您可以在一些 标记中包含一个链接,将用户指向诸如 example.com#foo?javascript=disabled 之类的内容。

不幸的是,浏览器不会报告 JS 是否启用,因此无法通过简单的 HTTP GET 知道您是否应该发送依赖 JS 的页面。

You could include a link inside some <NOSCRIPT> tags that point the user to something like example.com#foo?javascript=disabled.

Unfortunately, browsers do not report whether JS is enabled or not, so there's no way to know from a simple HTTP GET whether or not you should send JS reliant pages.

香橙ぽ 2024-10-09 09:11:08

您应该只构建一个 AJAX 查询来设置启用 JavaScript 的会话变量。

在加载站点上的任何其他信息之前运行此 AJAX 查询,然后执行简单的重定向到实际站点。

你可以做这样的伪代码:

Index.php:

ajax(check_js.php);
redirect(main_page.php);

check_js.php

$_SESSION['js_enable'] = true;

main_page.php

if($_SESSION['js_enable'] == true) {
  //execute page
} else {
  header("Location: no_js_error.php");
}

You should just build an AJAX query that sets a session variable for javascript enabled.

Run this AJAX query before any other information on the site is loaded and then do a simple redirect to the actual site.

You could do something like this pseudo code:

Index.php:

ajax(check_js.php);
redirect(main_page.php);

check_js.php

$_SESSION['js_enable'] = true;

main_page.php

if($_SESSION['js_enable'] == true) {
  //execute page
} else {
  header("Location: no_js_error.php");
}
离旧人 2024-10-09 09:11:08

与其让服务器尝试嗅探用户的设置,不如首先使用 unobtrusive javascript ?这样,如果 JS 不可用,页面将优雅地降级(到所需的状态)。

Instead of the server trying to sniff our the user's settings, how about using unobtrusive javascript in the first place? This way, the page will degrade gracefully (to the desired state) if JS is not available.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文