Table of Contents
yii2 How to load css and js at the bottom of the page, yii2 loads cssjs
Home Backend Development PHP Tutorial How to load css and js at the bottom of the page in yii2, yii2 loads cssjs_PHP tutorial

How to load css and js at the bottom of the page in yii2, yii2 loads cssjs_PHP tutorial

Jul 12, 2016 am 08:54 AM
css yii2 load and bottom page

yii2 How to load css and js at the bottom of the page, yii2 loads cssjs

Author: Bailang Source: www.manks.top/article/yii2_load_js_css_in_end

The copyright of this article belongs to the author, and you are welcome to reprint it. However, this statement must be retained without the author's consent, and a link to the original text must be provided in an obvious position on the article page. Otherwise, we reserve the right to pursue legal liability.

As the saying goes, destiny is created by oneself and appearance is born by the heart, so how do our problems arise?

This question is relatively old but is often asked by people. The reason is,

First of all, it is inevitable in actual development, and secondly, friends who are new to Yii like to hold on to it. That

Why do people who understand this problem not worry about it? Alien: Why are you still struggling with this issue when everyone knows how to do it?

That’s okay, just take a look at it for everyone involved.

Generally speaking, the js files or codes inside the web page are placed in front of the bottom of the web page . This is because the web page is loaded from top to bottom. When users visit our page, try not to load it because of js displays a blank page for too long, and if it stays for too long, users will be lost in vain.

Yii2 is integrated with jQuery, and the jQuery file is loaded at the bottom of the page. Therefore, if our js code snippet is not loaded at the bottom of the page, a $undefined friendly prompt is very likely to occur.

I have a lot of nonsense and I really need to get an account checked...

Let’s first take a look at how to deal with js code snippets

<?php
$this->registerJs('
$(function () {
//为所欲为的写你想要写的js代码吧
$......
});
', \yii\web\View::POS_END);
Copy after login

Yes, you just use the registerJs method above to register. Some friends don’t understand. What is registration? The simple understanding is to place your js code on the page where you want to place it.

The first parameter is easy to understand, it is the js code block we want to write. The second parameter is that we need to specify the specific location where the code block is inserted on the page.

The second parameter is only discussed here, yiiwebView::POS_END, which means it is inserted before at the bottom of the page.

Of course there is a third parameter, which means an id mark of the js code block. If not specified, it will be generated by default and is ignored here.

Oh, by the way, don’t be confused with $this above, it refers to the yiiwebView object

Let’s take a look at how to introduce external js files.

The example on the official website is as follows

$this->registerJsFile('http://example.com/js/main.js', ['depends' => [\yii\web\JqueryAsset::className()]]);
Copy after login

But people have said that we don’t recommend using it this way. It’s complicated to rely on it.

Okay, let’s take a look at how to use package management asset bundles to register.

Let’s open the file backendassetsAppAsset.php first and take a look at what it is. It’s really advanced. I looked like I couldn’t understand it at all. It’s over. I can’t write anything below. I can’t understand how to explain it. Back to the topic, we must seize the time to expand.

We added two static methods to the AppAsset class. The complete version of the AppAsset class is as follows:

namespace backend\assets;
use yii\web\AssetBundle;
/**
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class AppAsset extends AssetBundle
{
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css = [
        'css/site.css',
    ];
    public $js = [
    ];
    public $depends = [
        'yii\web\YiiAsset',
        'yii\bootstrap\BootstrapAsset',
    ];<br />    //定义按需加载JS方法,注意加载顺序在最后  
    public static function addScript($view, $jsfile) {  
        $view->registerJsFile($jsfile, [AppAsset::className(), 'depends' => 'backend\assets\AppAsset']);  
    }  
      
    //定义按需加载css方法,注意加载顺序在最后  
    public static function addCss($view, $cssfile) {  
        $view->registerCssFile($cssfile, [AppAsset::className(), 'depends' => 'backend\assets\AppAsset']);  
    }  
}
Copy after login

Let’s first talk about the function and intention of the added addScript and addCss. As mentioned above, it is not recommended to directly use the $this->registerJsFile method to register files in the view layer. Here, we add the addScript method. , in the future, the view layer will directly call this method to register the file.

Then why is this good? The benefits are very obvious. Calling this method avoids filling in dependencies in each registration file, which is very convenient.

It should be noted that the files that need to be registered will be behind the yii.js and bootstrap.js files, which is exactly what we need.

In this way, it is very simple for us to load external js files in the view layer, as shown below,

use backend\assets\AppAsset;
AppAsset::register($this);
AppAsset::addScript($this,'/css/main.js');
Copy after login

Without having to be as cumbersome as the following:

$this->registerJsFile('/css/main.js',['depends'=>['backend\assets\AppAsset'], 'position'=> $this::POS_END]);
$this->registerJsFile('/css/left.js',['depends'=>['backend\assets\AppAsset']]);
$this->registerJsFile('/css/extension.js',['depends'=>['backend\assets\AppAsset']]);
Copy after login

At this point, we have completely implemented loading css, js code or external files at the bottom of the page in yii2.

If you want to learn more, you can continue to refer to the official documentation.

Official document http://www.yiiframework.com/doc-2.0/guide-structure-assets.html

http://www.yiiframework.com/doc-2.0/guide-output-client-scripts.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1122393.htmlTechArticleyii2 How to load css and js at the bottom of the page, yii2 loads cssjs Author: Bailang Source: www.manks.top /article/yii2_load_js_css_in_end The copyright of this article belongs to the author. Reprinting is welcome, but without authorization from the author...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

See all articles