This article describes how to create a variable in the view (php code protection) in yii, and call the code instance of the previously created variable in the js file. if you need it, refer to the following code first:
This is in the View site/reg. php.
$cs = Yii::app()->getClientScript(); $cs->registerScriptFile('/js/verify-code.js',CClientScript::POS_END);
This is in 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);
This is in the verify-code.js file
var initSecond = (typeof CommonValue.WaitSecond) == "undefined" ? 180 : CommonValue.WaitSecond, waitSecond =initSecond, SmsVerifyCode = function(btn, form) { this.getBtn = btn; this.form = form;}, SCF;
This is an error reported by the chrom browser.
verify-code.js:1 Uncaught ReferenceError: CommonValue is not defined
Reply content:
Last found:
It was originally written
Yii::app()->clientScript->registerScript("var commonValue = $commonValue",CClientScript::POS_END);
A parameter is missing. The result is as follows:
Yii::app()->clientScript->registerScript('commonValue',"var commonValue = $commonValue",CClientScript::POS_END);
POS_END of commonValue is simply adjusted to POS_HEAD.
Or make sure that $ commonValue is placed in main. php before render ('Reg. php'). The two registerscripts are sequential.
Ps:
Can the verify-code.js return a function or object instead of referencing a global variable in this file?
I have previously maintained such code, which is disgusting.
The above is the content of the code instance that yii creates variables (protecting php code) in the view and calls the previously created variables in the js file, for more information, see PHP Chinese website (www.php1.cn )!