Home Backend Development PHP Tutorial yii2 How to load css and js at the bottom of the page css rounded corners css layout css definition

yii2 How to load css and js at the bottom of the page css rounded corners css layout css definition

Jul 29, 2016 am 08:52 AM
css yii

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 the original text link should be given in an obvious position on the article page, otherwise it will be retained 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,

Firstly, it is inevitable in actual development, and secondly, friends who are new to Yii like to hold on to it. Then

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

That’s okay, just take a look at it if it concerns everyone.

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 js If a blank page is displayed for too long, users will be lost if they stay for too long.

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 register for an account...

Let’s see how to deal with the js code snippet first

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

Yes, just use the registerJs method above to register, any friends are listening I don’t understand, what is registration? A simple understanding is to place your js code at the page location 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 given like this

$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, because it makes the dependence and dependence complicated.

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 look like I can’t understand it at all. It’s over. I can’t write anything below. I can’t understand how to explain it. Return. To get to the point, we need to 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 functions and intentions of the added addScript and addCss. As mentioned above, it is not recommended to use them in the view The layer directly uses the $this->registerJsFile method to register the file. Here, we added 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, like the following,

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

instead of being 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 it in yii2 Load css, js code or external files at the bottom of the page.

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

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

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

The above introduces how yii2 loads css and js at the bottom of the page, including CSS and yii content. I hope it will be helpful to friends who are interested in PHP tutorials.

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

What does placeholder mean in vue What does placeholder mean in vue May 07, 2024 am 09:57 AM

In Vue.js, the placeholder attribute specifies the placeholder text of the input element, which is displayed when the user has not entered content, provides input tips or examples, and improves form accessibility. Its usage is to set the placeholder attribute on the input element and customize the appearance using CSS. Best practices include being relevant to the input, being short and clear, avoiding default text, and considering accessibility.

What does span mean in js What does span mean in js May 06, 2024 am 11:42 AM

The span tag can add styles, attributes, or behaviors to text. It is used to: add styles, such as color and font size. Set attributes such as id, class, etc. Associated behaviors such as clicks, hovers, etc. Mark text for further processing or citation.

What does rem mean in js What does rem mean in js May 06, 2024 am 11:30 AM

REM in CSS is a relative unit relative to the font size of the root element (html). It has the following characteristics: relative to the root element font size, not affected by the parent element. When the root element's font size changes, elements using REM will adjust accordingly. Can be used with any CSS property. Advantages of using REM include: Responsiveness: Keep text readable on different devices and screen sizes. Consistency: Make sure font sizes are consistent throughout your website. Scalability: Easily change the global font size by adjusting the root element font size.

How to introduce images into vue How to introduce images into vue May 02, 2024 pm 10:48 PM

There are five ways to introduce images in Vue: through URL, require function, static file, v-bind directive and CSS background image. Dynamic images can be handled in Vue's computed properties or listeners, and bundled tools can be used to optimize image loading. Make sure the path is correct otherwise a loading error will appear.

What is node in js What is node in js May 07, 2024 pm 09:06 PM

Nodes are entities in the JavaScript DOM that represent HTML elements. They represent a specific element in the page and can be used to access and manipulate that element. Common node types include element nodes, text nodes, comment nodes, and document nodes. Through DOM methods such as getElementById(), you can access nodes and operate on them, including modifying properties, adding/removing child nodes, inserting/replacing nodes, and cloning nodes. Node traversal helps navigate within the DOM structure. Nodes are useful for dynamically creating page content, event handling, animation, and data binding.

What language is the browser plug-in written in? What language is the browser plug-in written in? May 08, 2024 pm 09:36 PM

Browser plug-ins are usually written in the following languages: Front-end languages: JavaScript, HTML, CSS Back-end languages: C++, Rust, WebAssembly Other languages: Python, Java

How to set unknown attributes in vscode vscode method to set unknown attributes How to set unknown attributes in vscode vscode method to set unknown attributes May 09, 2024 pm 02:43 PM

1. First, open the settings icon in the lower left corner and click the settings option. 2. Then, find the CSS column in the jumped window. 3. Finally, change the drop-down option in the unknownproperties menu to the error button.

What do ref and id in vue do? What do ref and id in vue do? May 02, 2024 pm 08:42 PM

In Vue.js, ref is used in JavaScript to reference a DOM element (accessible to subcomponents and the DOM element itself), while id is used to set the HTML id attribute (can be used for CSS styling, HTML markup, and JavaScript lookup).

See all articles