2013-12-13 35 views
0

我们正在使用Yii作为我们的项目。我试图在头文件中有条件地注册两个JS脚本/资产:一个用于IE8,一个用于其他浏览器 - 使用条件注释(例如<!--[if lte IE 8]>)。在Yii的控制器中显示自定义HTML <head>

但是,我只熟悉Yii::app()->clientScript->registerScriptFileYii::app()->clientScript->registerScript,其中没有一个公开了用条件注释包围已注册脚本的方法。

我试图直接做echo在控制器动作的开始:

echo '<!--[if lte IE 8]><script src="'.$assetsPath . '/charts/r2d3.js'.'" charset="utf-8"></script><![endif]-->'; 

然而,当我在看剧本时(之前甚至<html>)显示在文档的顶部源。如果可能,我想在<head>中显示它。

回答

1

您可以使用下面的扩展

http://www.yiiframework.com/extension/ewebbrowser/

我将文件放入组件中。然后只是在代码中做

$b = new EWebBrowser(); 
if($b->platform == "Internet Explorer" && $b->version == "8.0") {//you could explode version at the . I guess, however not sure if there is even a version 8.x or whatever 
    Yii::app()->clientScript->registerScriptFile("path/to/script"); 
} else { 
    Yii::app()->clientScript->registerScriptFile("path/to/otherscript"); 
} 

我用当前的IE,Firefox和Chrome浏览器测试过这个。我无法确保这可以与这些浏览器的其他版本一起使用。它两岁了,但它似乎仍在工作。

1

你可以在头下面设置此,

进入 - >则须─> layouts-> main.php

简单的添加你需要经过不断的内容,

<!DOCTYPE html> 
[if lt IE 7]><html class="no-js lt-ie9 lt-ie8 lt-ie7"><![endif] 
[if IE 7]><html class="no-js lt-ie9 lt-ie8"> <![endif] 
[if IE 8]><html class="no-js lt-ie9"> <![endif] 
[if gt IE 8]><html class="no-js"> <![endif] 
<head> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
</head> 

或者如果您需要检查浏览器版本,在控制器中并追加脚本。这种方式也会起作用。

在控制器定义此,

public function beforeRender($view) { 
//condition to check the browser type and version 
if($browser = 'ie' && $version = '8') { 
    Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . '/js/script.js'); 
    return true; 
} 
} 

//还没有测试的第二个选项。但应该工作。

您可以用以下任一检查浏览器类型和版本,

<?php 
echo $_SERVER['HTTP_USER_AGENT']; 

$browser = get_browser(null, true); 
print_r($browser); 
?> 

检查这个环节,http://php.net/manual/en/function.get-browser.php#101125

0

这不是最佳方法,但使用下面的代码就可以实现你的目标:如果你想以上特定控制器条件语句,然后

在你的布局/主。PHP

<?php if (Yii::app()->getController()->getId() == 'Your Controller Name'): ?> 
<!--[if lt IE 8]> 
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/ie.css" media="screen, projection" /> 
<![endif]--> 
<?php endif; ?> 

如果你想上面的条件语句特别控制器行动,则:

在你的布局/ main.php

<?php if (Yii::app()->getController()->getId() == 'Your Controller Name' && Yii::app()->getController()->getAction()->getId() == 'Your Action Name'): ?> 
<!--[if lt IE 8]> 
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/ie.css" media="screen, projection" /> 
<![endif]--> 
<?php endif; ?>