Writing JS and CSS in the same file

Source: Internet
Author: User

We often combine multiple CSS or multiple JavaScript codes to save requests, but this requires at least two. On the msdn blog, we can see that JS and CSS can also be combined into a file. What should we do? Using annotations, let's take a look at this Code .
<! --/*
Window. onload = function (){
Document. getelementbyid ("Test2"). innerhtml = "the legendary JS tester. If you see me, <strong> JS </strong> also takes effect ";
}
<! --*/
<! -- # Test1, # Test2 {border: 3px #000 solid; padding: 5px; color: #123456; Background-color: # CCC ;}strong {color: red ;}

CSS will be ignored during processing <! -- In this case, the JS part is only the comments of CSS/* js */, while the JS part is <! -- Convert to //, that is, the single-line comment of JS. The single-line comment will take out the following/* and */, and also take out CSS. Let's see the demo.

When running on the internetProgramOutput A "*/*" header. Otherwise, mime will not work in Some browsers. PHP is like this:

<? Header ('content-type: */* ');?>

Theoretically, it works to specify the mime with a certain suffix on the server as "*/*", but I didn't try it. Writing JavaScript and CSS in the same file is especially suitable for third-party calls. Although the file is a little larger, it can reduce the number of requests by half.

Of course, such a format must be difficult to write, but you can use the program to read the existing format and process it. In this way, the original file can be maintained conveniently, the output file saves some time for requests. If you want to handle the cache, it is similar to static files. Here I have C #, which is said to be official. I use PHP, because I have not written the cache mechanism, so I won't be embarrassed. In fact, the principle is also very simple, it can be programmed three or two times.

In fact, I want to find a way to merge multiple CSS files. It is not difficult to merge them. It can be difficult to handle the modular separation while caching the client and facilitating program calls, CSS is not like JavaScript code that can be called asynchronously on the client. Who can introduce it to me.

Legendary Test
The legendary tester 1. If you see that I have a border and a color, that is, CSS takes effect.

The legendary JS tester. If you see me, the JS will also take effect.

Copy code The Code is as follows: use the same file: jscss. php
<Link type = "text/CSS" rel = "stylesheet" href = "jscss. PHP "/> <SCRIPT type =" text/JavaScript "src =" jscss. PHP "> </SCRIPT> jscss. PHP content
<? Header ('content-type: */* ');?>
<! --/*
Window. onload = function (){
Document. getelementbyid ("Test2"). innerhtml = "the legendary JS tester. If you see me, <strong> JS </strong> also takes effect ";
}
<! --*/
<! -- # Test1, # Test2 {border: 3px #000 solid; padding: 5px; color: #123456; Background-color: # CCC ;}strong {color: red ;}
Copyright 2006-2007 aoao, some rights reserved.
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.