1

I'm using Yii::app()->clientScript to register script; In php file, I wrote script in multiply line, but when it register to browser, every script file will put on a line, include my comment, this will make error because everything after // will be know as comment.Script after transfer is something like this: tag.firstChild.onchange=function(){ load_grid(obj); }; } //Comment ....It has only one line.Please help me.

Update: This is an example:

$cs=Yii::app()->clientScript;           
    $cs->registerScriptFile('//maps.googleapis.com/maps/api/js?sensor=false', CClientScript::POS_HEAD); 

    $cs->registerScript('googlemaps_api','

    function geocode(address, lat_id, lng_id) {
        var geocoder = new google.maps.Geocoder();          

        geocoder.geocode({address: address}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {                                      
                $("#"+lat_id).val(results[0].geometry.location.lat());
                $("#"+lng_id).val(results[0].geometry.location.lng());
            } else alert("'.Yii::t('views/config/edit_store_locations_options','ERROR_GEOCODE_ADDRESS').'");        
        });                 
    }
    ', CClientScript::POS_HEAD);    
1
  • show us how exactly you register, and what is the content of the file being registered. Commented Mar 15, 2014 at 8:44

2 Answers 2

4

I find the embedding of inline JS code into my registerScript() function call very inconvenient, and opt to use heredoc format with success.

$script = <<<EOD

    function geocode(address, lat_id, lng_id) {
        var geocoder = new google.maps.Geocoder();          

        geocoder.geocode({address: address}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {                                      
                $("#"+lat_id).val(results[0].geometry.location.lat());
                $("#"+lng_id).val(results[0].geometry.location.lng());
            } else alert("'.Yii::t('views/config/edit_store_locations_options','ERROR_GEOCODE_ADDRESS').'");        
        });                 
    }

EOD;

    Yii::app()->clientScript->registerScript('googlemaps_api', $script, CClientScript::POS_HEAD);

?>
Sign up to request clarification or add additional context in comments.

Comments

1

Well that is normal and somewhat desired behavior. Why not use /**/ to put in your comments, in this way everything will work properly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.