

The variable $message is created in PHP and Preferred way of writing inline JavaScript, especially useful for code that is longer than a single line. This also enables better syntax highlighting in most IDEs so it is the The registerJs() method for adding these can be used as follows: $this->registerJs( Inline scripts are useful for configuration, dynamically generated code and small snippets created by reusable frontend code contained in widgets. There are two dedicated methods for this:


When working with the yii\web\View object you can dynamically register frontend scripts. This section describes methods provided by Yii for adding JavaScript and CSS to a website as well as dynamically adjusting these. To modify the page in the browser by manipulating existing elements or Rendered and sent to the browser, contain JavaScript that is used This ensures a single entry point, since the data-main script you specify is loaded asynchronously.Modern web applications, additionally to static HTML pages that are Inside of main.js, you can use requirejs() to load any other scripts you need to run. For browsers that support it, you could also add an async attribute to the script tag. You could also place the script tag end of the section if you do not want the loading of the require.js script to block rendering. To take full advantage of the optimization tool, it is suggested that you keep all inline script out of the HTML, and only reference require.js with a requirejs call like so to load your script: For example, if you have a project that has a project.html page, with some scripts, the directory layout might look like so:Īdd require.js to the scripts directory, so it looks like so: This setup assumes you keep all your JavaScript files in a "scripts" directory in your project. Add RequireJS § 2 Note: For jQuery-specific advice, see the jQuery integration page
