Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

[·­Òë]High Performance JavaScript(004)

XMLHttpRequest Script Injection  XHR½Å±¾×¢Èë
    Another approach to nonblocking scripts is to retrieve the JavaScript code using an XMLHttpRequest (XHR) object and then inject the script into the page. This technique involves creating an XHR object, downloading the JavaScript file, then injecting the JavaScript code into the page using a dynamic <script> element. Here's a simple example:
    ÁíÒ»¸öÒÔ·Ç×èÈû·½Ê½»ñµÃ½Å±¾µÄ·½·¨ÊÇʹÓÃXMLHttpRequest(XHR)¶ÔÏ󽫽ű¾×¢Èëµ½Ò³ÃæÖС£´Ë¼¼ÊõÊ×ÏÈ´´½¨Ò»¸öXHR¶ÔÏó£¬È»ºóÏÂÔØJavaScriptÎļþ£¬½Ó×ÅÓÃÒ»¸ö¶¯Ì¬<script>ÔªËؽ«JavaScript´úÂë×¢ÈëÒ³Ãæ¡£ÏÂÃæÊÇÒ»¸ö¼òµ¥µÄÀý×Ó£º
var xhr = new XMLHttpRequest();
xhr.open("get", "file1.js", true);
xhr.onreadystatechange = function(){
  if (xhr.readyState == 4){
    if (xhr.status >= 200 && xhr.status < 300 || xhr.status == 304){
      var script = document.createElement_x("script");
      script.type = "text/javascript";
      script.text = xhr.responseText;
      document.body.appendChild(script);
    }
  }
};
xhr.send(null);
    This code sends a GET request for the file file1.js. The onreadystatechange event handler checks for a readyState of 4 and then verifies that the HTTP status code is valid (anything in the 200 range means a valid response, and 304 means a cached response). If a valid response has been received, then a new <script> element is created and its text property is assigned to the responseText received from the server. Doing so essentially creates a <script> element with inline code. Once the new <script> element is added to the document, the code is executed and is ready to use.
    ´Ë´úÂëÏò·þÎñÆ÷·¢ËÍÒ»¸ö»ñÈ¡file1.jsÎļþµÄGETÇëÇó¡£onreadystatechangeʼþ´¦Àíº¯Êý¼ì²éreadyStateÊDz»ÊÇ4£¬È»ºó¼ì²éHTTP״̬ÂëÊDz»ÊÇÓÐЧ£¨2XX±íʾÓÐ


Ïà¹ØÎĵµ£º

HTML+Javascript+XML


ÉÏÃæËù˵ÓйØHTMLµÄÄÚÈݷdz£ÉÙÓÖ¼òµ¥,µ«¶ÔÒѾ­Á˽âµÄÈËÀ´Ëµ¾ÍÊÇûÓõÄ.
ÈçÓÐÎÊÌâ¿Éµ½È¨ÍþÍø http://www.html.com/ Éϲ鿴
ÒÔÏ¿ªÊ¼ËµËµ¹ØÓÚXMLµÄһЩ֪ʶ.
XMLÒ²ÊDZê¼ÇÓïÑÔ,¿ÉËüÊÇ×Ô¶¨ÒåµÄ,ûÓÐÒѸø¶¨¸ñʽ.²»¾ßÌå˵Ëü,¸ø³öÀý×ӾͿÉÃ÷ÁË.
Èç
<NAME>TOM</NAME>
<SEX>M</SEX>
ÒÔÉÏÄÚÈݵÄ< ......

Ò»¼ÒÖ®ÑÔ£ºËµËµ JavaScript µÄÀà³ÉÔ±µÄ·ÃÎÊ¿ØÖÆ

¿´ÊéµÄʱºòÓöµ½ÕâÑùÒ»¸öÎÊÌ⣬³ÌÐò´úÂëÈçÏÂ
var ob = function(){
var obj = this;
function fn1(){
alert( obj === window );//false
alert( this === window );//ture
}
this.fn2 = function() {
fn1();
}

µ±Ê±ºÜ²»Ã÷°×fn1ÀïÃæµÚ¶þ¸öalertµÄ½á¹û£¬Îª ......

JavaScriptÀûÓÃDOM¸øÒ³ÃæÌí¼ÓÄÚÈÝ

×Ô¶¨ÒåÒ»¸ölogº¯Êý,Êä³ö´«È뺯ÊýµÄ¶ÔÏó»òÕßÐÅÏ¢.
Log.js
// JScript source code
function
log(category, message, object) {
// If this category is explicitly disabled, do nothing
if
(log.options[category + "Disabled
"]) return
;
// Find the container
var
id = categ ......

JavascriptµÄÔ­ÐͶÔÏóºÍprototypeÊôÐÔ

     JavascriptÖеļ̳лúÖÆÊÇËùνµÄÔ­ÐͶÔÏó¼Ì³Ð£¬Í¨¹ýÊôÐÔ·ÃÎÊ»úÖƵÄÌØÊâÐÔÀ´ÊµÏּ̳еġ£Ö®Ç°ÎÒÒ»Ö±ÒÔΪËùÓжÔÏóµÄÔ­ÐͶÔÏó¾ÍÊÇprototypeËùÖ¸µÄÄǸö¶ÔÏ󡣺óÀ´ÔÚ¿´javascriptÉè¼ÆģʽµÄʱºò·¢ÏÖ×÷Õß×¢ÖÐÓÐÒ»¾ä»°ÓëÎÒ֮ǰµÄÀí½â²»Í¬£¬Ëû˵“ÿ¸ö¶ÔÏó¶¼ÓÐÒ»¸öÔ­ÐͶÔÏ󣬵«Õâ²¢²»Òâζ×Åÿ¸ö¶ÔÏó¶ ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ