What is the best way to register js code in yii2 view?
<?php
$this->registerJs(
'$("document").ready(function(){ alert("hi"); });'
);
?>
<?php
$this->registerJs('alert("hi");', View::POS_READY);
?>
<?php
$script = "function test() { alert('hi');}";
$this->registerJs($script, View::POS_END, 'my-options');
?>
Yii2, write code in views
<h2>Content</h2>
<?php
$script = <<< JS
alert("Hi");
JS;
$this->registerJs($script);
?>
<?php
$this->registerJs( <<< EOT_JS_CODE
// JS code here
EOT_JS_CODE
);
?>
So you have not to escape js code
https://www.yiiframework.com/doc/guide/2.0/en/output-client-scripts
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With