Yii creates a variable in the view (the code that protects PHP) and invokes the code instance of the previously created variable in the JS file.

Source: Internet
Author: User
Tags yii
First on the code:

This is the view site/reg.php.

    $cs = Yii::app ()->getclientscript ();    $cs->registerscriptfile ('/js/verify-code.js ', Cclientscript::P os_end);

This is layout/main.php.

$commonValue = Json_encode ([            ' smsverifycodeurl ' = + Yii::app ()->createabsoluteurl (' Site/sendsmscode '),            ' Waitsecond ' = wapmember::D efault_interval,    ]); Yii::app ()->clientscript->registerscript ("var commonvalue = $commonValue", Cclientscript::P os_end);

This is the Verify-code.js file.

var initsecond = (typeof commonvalue.waitsecond) = = "undefined"? 180:commonvalue.waitsecond,    waitsecond =initsecond,    smsverifycode = function (btn, form) {this.getbtn = btn; th Is.form = form;},       SCF;

This is Chrom's browser error.
verify-code.js:1 Uncaught ReferenceError: CommonValue is not defined

Reply content:

Finally found:
It was written in the original

Yii::app ()->clientscript->registerscript ("var commonvalue = $commonValue", Cclientscript::P os_end);

One parameter is missing. The result is such a drop:

Yii::app ()->clientscript->registerscript (' Commonvalue ', "var commonvalue = $commonValue", Cclientscript::P os_ END);

The pos_end of Commonvalue is simply adjusted to Pos_head.

Or make sure the $commonValue is placed in main.php before render (' reg.php '), two times Registerscript is in sequence

Ps:

Verify-code.js can I return a function or an object instead of referencing a global variable in this file?
Had previously maintained such a code, disgusting a ratio of

The above is yii in the view to create a variable (the code to protect PHP), in the JS file to invoke the previously created variables of the code instance of the content, more relevant content please pay attention to topic.alibabacloud.com (www.php.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.