我正在尝试使用Yii加载XML / SWF图表并遇到一些麻烦。据我所知,问题似乎是Yii在标题中加载脚本标签的顺序。
这是我的工作test.php脚本(减去Yii)生成的标题所得到的源代码。
<HTML>
<script language="javascript">
/*<![CDATA[*/
AC_FL_RunContent = 0;
DetectFlashVer = 0;
var requiredMajorVersion = 10;
var requiredMinorVersion = 0;
var requiredRevision = 45;
/*]]>*/
</script>
<script src="AC_RunActiveContent.js" language="javascript"></script>
<BODY bgcolor="#FFFFFF">
现在我正试图让Yii使用这段代码完成同样的事情。
<?php
Yii::app()->clientScript->registerScript('AC_FL_RunContent', 'AC_FL_RunContent = 0;', CClientScript::POS_HEAD);
Yii::app()->clientScript->registerScript('DetectFlashVer', 'DetectFlashVer = 0;', CClientScript::POS_HEAD);
Yii::app()->getClientScript()->registerScriptFile($chartUrl.'/AC_RunActiveContent.js');
Yii::app()->clientScript->registerScript('DetectFlashVer',
'
var requiredMajorVersion = 10;
var requiredMinorVersion = 0;
var requiredRevision = 45;
', CClientScript::POS_HEAD);
?>
这就是我得到的
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="language" content="en" />
<link rel="stylesheet" type="text/css" href="/css/main.css" />
<link rel="stylesheet" type="text/css" href="/css/form.css" />
<link rel="stylesheet" type="text/css" href="/css/components.css" />
<link rel="stylesheet" type="text/css" href="/css/pages.css" />
<script type="text/javascript" src="/assets/e5f807c3/AC_RunActiveContent.js"></script>
<script type="text/javascript">
/<![CDATA[/
AC_FL_RunContent = 0;
DetectFlashVer = 0;
var requiredMajorVersion = 10;
var requiredMinorVersion = 0;
var requiredRevision = 45;
/]]>/
</script>
</head>
<body>
问题似乎是加载AC_RunActiveContent.js的行的位置。当我将它移动到我的测试脚本中的其他变量之上时,它会破坏测试脚本。有没有办法强迫Yii按我想要的顺序编写命令?
更新8/23 / 10-已解决!!!
根据Gray Teardrop的建议,我能够实现这一目标。鉴于我现在已经挣扎了几天,我以为我会为其他人发布完整的解决方案。
首先,我在protected/vendors/Maani
show.php(包含图表的视图)
<?php
$chartPath=Yii::getPathOfAlias('application.vendors.Maani.*');
$chartUrl=Yii::app()->getAssetManager()->publish($chartPath);
Yii::app()->clientScript->registerScript('AC_FL_RunContent', 'AC_FL_RunContent = 0;', CClientScript::POS_HEAD);
Yii::app()->clientScript->registerScript('DetectFlashVer', 'DetectFlashVer = 0;', CClientScript::POS_HEAD);
Yii::app()->clientScript->registerScript('Morestuff',
'
var requiredMajorVersion = 10;
var requiredMinorVersion = 0;
var requiredRevision = 45;
', CClientScript::POS_HEAD);
Yii::app()->getClientScript()->registerScriptFile($chartUrl.'/AC_RunActiveContent.js',CClientScript::POS_BEGIN);
?>
<?php $chart = "<chart><chart_type>bar</chart_type><chart_border color='FF0000' /></chart>"; ?>
<script language="JavaScript" type="text/javascript">
/<![CDATA[/
if (AC_FL_RunContent == 0 || DetectFlashVer == 0) {
alert("This page requires AC_RunActiveContent.js.");
} else {
var hasRightVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
if(hasRightVersion) {
AC_FL_RunContent(
'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,45,2',
'width', '400',
'height', '250',
'scale', 'noscale',
'salign', 'TL',
'bgcolor', '#777788',
'wmode', 'opaque',
'movie', 'charts',
'src', '<?php echo $chartUrl; ?>/charts',
'FlashVars', "library_path=<?php echo $chartUrl; ?>/charts_library&xml_data=<?php echo $chart; ?>",
'id', 'my_chart',
'name', 'my_chart',
'menu', 'true',
'allowFullScreen', 'true',
'allowScriptAccess','sameDomain',
'quality', 'high',
'align', 'middle',
'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
'play', 'true',
'devicefont', 'false'
);
} else {
var alternateContent = 'This content requires the Adobe Flash Player. '
+ '<u><a href=http://www.macromedia.com/go/getflash/>Get Flash</a></u>.';
document.write(alternateContent);
}
}
/]]>/
请注意,src
和FlashVars
的相对路径信息是必需的。
答案 0 :(得分:2)
据我所知,CClientScript
保持通过registerScript()
注册的脚本块和通过registerScriptFile()
注册的脚本的单独顺序。但它始终会在registerScript()
之后呈现registerScriptFile()
。有两种方法可以完成您的任务:
CClientScript::POS_HEAD
中放置脚本块,在CClientScript::POS_END
中放置脚本链接。CClientScript
继承您自己的脚本管理器。