Yii creates variables in the view (protecting php code), and calls the code instance of the previously created variables in the js file.

Source: Internet
Author: User
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 )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.