2013-12-13 7 views
0

우리 프로젝트에 Yii를 사용하고 있습니다. 조건부 주석 (예 : <!--[if lte IE 8]>)을 사용하여 두 개의 JS 스크립트/애셋을 헤더에 조건부로 등록하려고합니다. 즉, IE8과 다른 브라우저 용으로 하나씩 등록하려고합니다.사용자 정의 HTML 표시 <head> Yii의 컨트롤러에서

그러나 나는 단지 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/

I을 사용할 수 있습니다 파일을 구성 요소에 넣습니다. 그럼 그냥 내가 현재 IE, 파이어 폭스와 크롬 브라우저 테스트에게 이런 짓을 코드

$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"); 
} 

에서 할. 이 브라우저의 다른 버전에서도 작동하는지 확인할 수는 없습니다. 2 살이지만 아직도 일하는 것 같습니다.

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; 
} 
} 

// havent 한 두 번째 옵션을 테스트 컨트롤러이 정의한다. 그러나 일해야합니다. 다음 중 하나와 브라우저 종류와 버전을 확인할 수 있습니다

,

<?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; ?> 
에서