javascript - 如何检测 IE9 或更低版本,然后根据结果执行一些 PHP?

标签 javascript php internet-explorer-9

我知道如何使用 javascript 检测浏览器,但如何将此信息传递给 PHP?

我该如何执行以下操作:

  1. Javascript 检测浏览器,如果用户使用的是 IE 9 或更低版本,则返回“yes”,然后转到步骤 2。

  2. Javascript 向 PHP 传达"is"并转到步骤 3。

  3. PHP 根据上述 javascript 中是否存在“yes”来执行 if/else 语句。

最佳答案

用户代理不是可靠的方法。
就我个人而言,我经常使用代理切换器来愚弄网站。
这个方法不会被骗。
我唯一的选择是禁用 JavaScript。

选择适用于 IE9+ 而不适用于早期版本的 JS 指令

除了 getElementsByClassName 之外,可能还有更好的指令可以使用,但它演示了这个概念。

从测试页面中选择一个指令,如下所示:

Browser Capability Tests

然后构建一个测试页面并在跨浏览器测试站点上测试该页面。

<小时/>

此测试将正确通过 (TRUE) 或失败 (FALSE),具体取决于 1999 年以来任何浏览器上对 try{}catch{} 的支持:

  • 网景 6.0
  • FireFox 1.0
  • IE 5.5

它将通过 TRUE:

  • Chrome
  • FireFox 3.0+
  • IE9+
  • 歌剧
  • Safari

仅在 IE5.5 - IE8 上会失败 (FALSE)

<小时/>

HTML

<div class="test"></div>

JS

var checkIE = false;
try {
   var el = document.getElementsByClassName('test');
   checkIE9 = true;
}
catch(e)  {
  checkIE9 = false;
}

然后根据 checkIE9 的真假进行重定向

用 IE9 页面/脚本替换 mozilla.org,用旧样式页面/脚本替换 microsoft.com

if (checkIE9){
  window.location = 'http://mozilla.org';
}
else{
  window.location = 'http://www.microsoft.com';
}
<小时/>

或根据真假提交表单

HTML

<form action="true.php" method="get"><div>
  <input type="hidden" name="chkIE" value="1"/>
  <button id="t" type="submit"></button>
</div></form>

<form action="false.php" method="get"><div>
  <input type="hidden" name="chkIE" value="2"/>
  <button id="f" type="submit"></button>
</div></form>

注意:是的,它可以是一个带有两个按钮的表单,但表单很便宜,而且我喜欢通过隐藏输入传递值。

我没有使用零作为值,以防用户直接转到表单链接到的页面。

test.php 中的零检查代码:

$chckIE9 = intval($_GET['chkIE9']);
if($chckIE9 == 0){
  include('test.html');
  exit;
}

或者,如果您可能需要一个中间页面并将 true 和 false 链接到同一脚本。

<form action="test.php" method="get"><div>
  <input type="hidden" name="chkIE" value="1"/>
  <button id="t" type="submit"></button>
</div></form>

<form action="test.php" method="get"><div>
  <input type="hidden" name="chkIE" value="2"/>
  <button id="f" type="submit"></button>
</div></form>

PHP:

$chckIE9 = intval($_GET['chkIE9']);
if($chckIE9 == 1){
  include('true.php');
}
elseif($chckIE9 == 2){
  include('false.php');
}
else{
   include('test.html');
}
exit;

JS
这里必须决定默认的方式。
您可以单击“f”如果 (!checkIE9)
那么默认就是IE9+
如果需要默认,最好默认为 false

if (checkIE9){ 
  document.getElementById('t').click();
}
else{
  document.getElementById('f').click();
}
<小时/>

代码片段

此代码段将显示“IE9+ 兼容”或“更新您的浏览器”
此代码使用 IE8 进行测试,会弹出“更新您的浏览器”

var checkIE = false;
try {
   var el = document.getElementsByClassName('true');
   checkIE9 = true;
}
catch(e)  {
  checkIE9 = false;
}
if (checkIE9){
  document.getElementById('t').style.display = 'block';
}
else{
  document.getElementById('f').style.display = 'block';
} 
#t,#f,.hide{display:none;}
<!DOCTYPE html>
<html lang="en">
<head></head><body>
<div id="t" class="true"><h2>&#x2003;IE9+ Compatible</h2></div>
<div id="f"><h2>&#x2003;UPDATE YOUR BROWSER</h2></div>
</div>
<form action="test.php" method="get"><div><input type="hidden" name="chkIE" value="1"/><button class="hide" id="t" type="submit"></button></div></form>
<form action="test.php" method="get"><div><input type="hidden" name="chkIE" value="2"/><button class="hide" id="f" type="submit"></button></div></form>
</body></html>

关于javascript - 如何检测 IE9 或更低版本,然后根据结果执行一些 PHP?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29782735/

相关文章:

javascript - 使用导航 html5 查询 javascript 隐藏显示 div

javascript - 文本选择 Internet Explorer

php - mysql 选择行优化星号或名称

jquery - 为什么下拉菜单在 IE9 中不起作用?

html - IE9 和 :after/:hover Css property causing page to grow

javascript - 从弹出窗口内容 Bootstrap ajax 调用

javascript - 获取高度动画后的回调

javascript - 在 Chart.js 行中显示数据集标签

javascript - 根据语言更改 PHP/JS 中的 CSS 值/属性

php - 如何获取正在电影院播放的所有电影?