先上代码:
$cs = Yii::app()->getClientScript();
$cs->registerScriptFile('/js/verify-code.js',CClientScript::POS_END);这是layout/main.php里
$commonValue = json_encode([
'SmsVerifycodeUrl' => Yii::app()->createAbsoluteUrl('site/SendSmsCode'),
'WaitSecond' => WapMember::DEFAULT_INTERVAL,
]);
Yii::app()->clientScript->registerScript("var commonValue = $commonValue",CClientScript::POS_END);这是verify-code.js文件里
var initSecond = (typeof CommonValue.WaitSecond) == "undefined" ? 180 : CommonValue.WaitSecond,
waitSecond =initSecond,
SmsVerifyCode = function(btn, form) { this.getBtn = btn; this.form = form;},
SCF;这是chrom浏览器报的错verify-code.js:1 Uncaught ReferenceError: CommonValue is not defined
最后发现:
原来是在写
立即学习“PHP免费学习笔记(深入)”;
Yii::app()->clientScript->registerScript("var commonValue = $commonValue",CClientScript::POS_END);少了一个参数。结果是这样子滴:
Yii::app()->clientScript->registerScript('commonValue',"var commonValue = $commonValue",CClientScript::POS_END);commonValue 的 POS_END 简单地调整为 POS_HEAD.
或者确定 $commonValue 在main.php 中放到 render('reg.php') 之前, 两次 registerScript 是有先后顺序的
ps:
verify-code.js 能不能返回函数或对象, 而不是在这个文件中引用全局变量?
之前维护过这样的代码,恶心的一比
以上就是yii在视图里创建变量(保护php的代码),在js文件里调用之前创建的变量的代码实例的内容,更多相关内容请关注PHP中文网(www.php.cn)!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号