CSS Loading Scheme for JS Components

Source: Internet
Author: User
During component development, there is usually a CSS file resource.
However, in practice, to reduce the number of requests, this CSS may be merged into the CSS of the page.
So, do I need to include this CSS when writing components?

Response Policy:
Add. Js_xcomponent_css_loadedTo identify whether the CSS corresponding to the component has been loaded.
In the component JS, make a judgment. If CSS is not pre-loaded (including independent pre-loading, or pre-loading is included in other CSS files), load it.
TypicalCodeAs follows:

 <  Html  >  
< Head >
< Link Hrefffff = "Dddd/panel.css" REL = "Stylesheet" Type = "Text/CSS" > <! -- Not referenced here -->
< Style >
. Js_panel_css_loaded { Width : 30px ; Display : None ; } /* Use ". js_panel_css_loaded" to indicate that the CSS corresponding to the Panel already exists. */
. Js_panel { /* More CSS */ }
</ Style >
</ Head >
< Body >
</ Body >
< Script Language = "JavaScript" Remark = "Diagram of CSS-related code in the panel component" >
( Function (){
/* *
* Obtain the current style of the element object.
* @ Method getcurrentstyle
* @ Param {element | string | wrap} El Id, element instance or wrap
* @ Param {string} attribute style name
* @ Return {string}
*/
VaR Getcurrentstyle = Function (EL, attribute ){
If (EL. currentstyle ){
Return El. currentstyle [attribute];
} Else {
VaR Style = El. ownerdocument. defaultview. getcomputedstyle (El, Null );
Return Style ? Style. getpropertyvalue (attribute ): Null ;
}
};

VaR El = Document. createelement ( ' Div ' );
Document. Body. appendchild (EL );
El. classname = ' Js_panel_css_loaded ' ;
If (Getcurrentstyle (El, ' Width ' ) = ' 30px ' ) Alert ( ' Panel_css has been loaded and does not need to be loaded. ' );
Else Alert ( ' Panel_css needs to be loaded ' );
Document. Body. removechild (EL );
}());
</ Script >
</ Html >
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.