[·Òë]High Performance JavaScript(003)
Dynamic Script Elements ¶¯Ì¬½Å±¾ÔªËØ
The Document Object Model (DOM) allows you to dynamically create almost any part of an HTML document using JavaScript. At its root, the <script> element isn't any different than any other element on a page: references can be retrieved through the DOM, and they can be moved, removed from the document, and even created. A new <script> element can be created very easily using standard DOM methods:
Îĵµ¶ÔÏóÄ£ÐÍ£¨DOM£©ÔÊÐíÄãʹÓÃJavaScript¶¯Ì¬´´½¨HTMLµÄ¼¸ºõÈ«²¿ÎĵµÄÚÈÝ¡£Æä¸ù±¾ÔÚÓÚ£¬<script>ÔªËØÓëÒ³ÃæÆäËûÔªËØÃ»ÓÐʲô²»Í¬£ºÒýÓñäÁ¿¿ÉÒÔͨ¹ýDOM½øÐмìË÷£¬¿ÉÒÔ´ÓÎĵµÖÐÒÆ¶¯¡¢É¾³ý£¬Ò²¿ÉÒÔ±»´´½¨¡£Ò»¸öеÄ<script>ÔªËØ¿ÉÒԷdz£ÈÝÒ×µØÍ¨¹ý±ê×¼DOMº¯Êý´´½¨£º
var script = document.createElement_x("script");
script.type = "text/javascript";
script.src = "file1.js";
document.getElementsByTagName("head")[0].appendChild(script);
This new <script> element loads the source file file1.js. The file begins downloading as soon as the element is added to the page. The important thing about this technique is that the file is downloaded and executed without blocking other page processes, regardless of where the download is initiated. You can even place this code in the <head> of a document without affecting the rest of the page (aside from the one HTTP connection that is used to download the file).
еÄ<script>ÔªËØ¼ÓÔØfile1.jsÔ´Îļþ¡£´ËÎļþµ±ÔªËØÌí¼Óµ½Ò³ÃæÖ®ºóÁ¢¿Ì¿ªÊ¼ÏÂÔØ¡£´Ë¼¼ÊõµÄÖØµãÔÚÓÚ£ºÎÞÂÛÔںδ¦Æô¶¯ÏÂÔØ£¬ÎļþµÄÏÂÔØºÍÔËÐж¼²»»á×èÈûÆäËûÒ³Ãæ´¦Àí¹ý³Ì¡£ÄãÉõÖÁ¿ÉÒÔ½«ÕâЩ´úÂë·ÅÔÚ<head>²¿·Ö¶ø²»»á¶ÔÆäÓಿ·ÖµÄÒ³Ãæ´úÂëÔì³ÉÓ°Ï죨³ýÁËÓÃÓÚÏÂÔØÎļþµÄHTTPÁ¬½Ó£©¡£
When a file is downloaded using a dynamic script node, the retrieved code is typically executed immediately (except in Firefox and Opera, which will wait until any previous dynamic script nodes have executed). This works well when the script is self-executing but
Ïà¹ØÎĵµ£º
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; ......
1. ¶¨ÒåÒ»¸öÓÃÓÚÊä³öJavaScript´úÂë µÄServletÀà¡£
package com.mycompany.response.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServle ......
ÒÔǰ×öÏîĿʱºò¾³£Óõ½¶àÐÐÊäÈë¿ò×ÖÊýµÄ¿ØÖÆÒÔ¼°Í³¼ÆÏÔʾµÄÇé¿ö£¬×ÛºÏÁ˼¸ÖÖÇé¿öºóÏÖÔڹ̶¨ÓÃÁËÕâ¸ö½Å±¾¡£
ÀýÈç ÒªÊäÈëÃèÊötextbox
<div>
<h3>
ÃèÊö£º</h3>
<asp:TextBox runat="server" ID="tbdesciption" Height="200px" TextMode="MultiLine" onkey ......
×Ô¶¨ÒåÒ»¸ö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Éè¼ÆÄ£Ê½µÄʱºò·¢ÏÖ×÷Õß×¢ÖÐÓÐÒ»¾ä»°ÓëÎÒ֮ǰµÄÀí½â²»Í¬£¬Ëû˵“ÿ¸ö¶ÔÏó¶¼ÓÐÒ»¸öÔÐͶÔÏ󣬵«Õâ²¢²»Òâζ×Åÿ¸ö¶ÔÏó¶ ......