ExistingWebIn order to improve the user experience and reuse of foreground scripts, applications often use a large number of scripts. When a page is displayed, hundredsKScript files are becoming more and more common.HttphandlerSimulate a script file that needs to be loaded for a long time.
Scripts. ashx
Public ClassScripts: ihttphandler {
Public VoidProcessrequest (httpcontext context ){
Context. response. contenttype ="Application/X-Javascript";
System. Threading. thread. Sleep (1500 );
Context. response. Write ("//");
}
Public BoolIsreusable {
Get{Return False;}
}
}
Load it using traditional Loading Methods,
<Html Xmlns=Http://www.w3.org/1999/xhtml" >
<Head Runat="Server" ID="AAA">
title untitled page title >
script type = " text/JavaScript " language = " Java Script " SRC = " scripts. ashx? A "> script >
<Script Type="Text/JavaScript" Language="JavaScript" SRC="Scripts. ashx? B "> </Script>
PassIEWe can see that loading these scripts is performed sequentially, and the loading time of multiple large scripts will grow exponentially.,It takes tens of seconds for the server to start a website outside China and users with poor network conditions.
To break the traditional page loading methodDocument. Write ()To dynamically load the script
HTML xmlns = " http://www.w3.org/1999/xhtml "
<Head Runat="Server" ID="AAA">
<Title>Untitled page</Title>
<Script Type="Text/JavaScript" Language="JavaScript">
Document. Write (
'<SCRIPT type ="Text/JavaScript"Language ="Javascript"'+
'Src ="Scripts. ashx? A"> <'+'/SCRIPT> ');
Document. Write (
'<SCRIPT type ="Text/JavaScript"Language ="Javascript"'+
'Src ="Scripts. ashx? B"> <'+'/SCRIPT> ');
</Script>
</Head>
Load the script in this wayIEWe will find that script loading is parallel, although it still cannot be broken throughHTTPADomainOnly two connections are allowed, but the Script Loading Speed is much faster..
The browser strictly determines the domain name. The subdomain name under the same domain name and the ports under the same domain name are different. It is common to increase the number of concurrent connections by adding subdomain names.
You can modifyHostFile to simulate concurrent multi-connection download.
HostFile
127.0.0.1 gms.test.91.com
127.0.0.1 gms1.test.91.com
127.0.0.1 gms2.test.91.com
<Html Xmlns=Http://www.w3.org/1999/xhtml" >
<Head Runat="Server">
<Title>Untitled page</Title>
<Script Type="Text/JavaScript" Language="JavaScript">
Document. Write ('<SCRIPT type ="Text/JavaScript"Language ="Javascript"'+
'Src ="Http: // gms.test.91.com/scripts.ashx? A"> <'+'/SCRIPT> ');
Document. Write ('<SCRIPT type ="Text/JavaScript"Language ="Javascript"'+
'Src ="Http://gms1.test.91.com/scripts. ashx? B"> <'+'/SCRIPT> ');
Document. Write ('<SCRIPT type ="Text/JavaScript"Language ="Javascript"'+
'Src ="Http://gms2.test.91.com/Scripts.ashx? C"> <'+'/SCRIPT> ');
</SCRIPT>
PassIEAs you can see, load3Requirements1.5 sThe completion time script is almost2 S.
Finally, let's talk about it. Defer This attribute ,<SCRIPT defer = "Defer"> </SCRIPT> When using some third-party script controls, an error is reported during the loading process. Internet Explorer Unable to open site, operation terminated, added Defer After the attribute, no error is reported. I checked the relevant information, Defer The function of the attribute is to tell the browser that the script does not exist and needs to be executed immediately.CodeAnd the script can be downloaded in the background in parallel with the page display, Defer In Firefox Is not supported.