首页 > 代码库 > 将參数从PHP传递到JavaScript中

将參数从PHP传递到JavaScript中

php:
	//自己定义数组參数
	$newarr = array(‘a1‘ => ‘a1‘, ‘a2‘ => ‘a2‘, ‘a3‘ => ‘a3‘);
	$config = CJavaScript::encode($newarr);
	Yii::app()->clientScript->registerScript(‘customConfig‘, "var customConfig = " . $config . ";", CClientScript::POS_HEAD);

	//yii 自带配置文件
	$yiiConfig = CJavaScript::encode(Yii::app()->params->toArray());
	Yii::app()->clientScript->registerScript(‘appConfig‘,"var yiiConfig = " . $yiiConfig . ";", CClientScript::POS_HEAD);

	//分配变量
	Yii::app()->clientScript->registerScript(‘myparam‘, ‘var abc = "Hello"‘, CClientScript::POS_HEAD);
	    
js:
	alert(customConfig.a1);
	alert(yiiConfig.alert.msg);
	alert(abc);


ps : 尽管yii 也有这样的类似功能,可是个人感觉还是没有druopal中实现的好!

将參数从PHP传递到JavaScript中