Maison >interface Web >js tutoriel >Code:loadScript( )加载js的功能函数_javascript技巧

Code:loadScript( )加载js的功能函数_javascript技巧

WBOY
WBOYoriginal
2016-05-16 19:19:461264parcourir
复制代码 代码如下:

<script> <BR>/** <BR> * function loadScript <BR> * Copyright (C) 2006 Dao Gottwald <BR> * <BR> * This library is free software; you can redistribute it and/or <BR> * modify it under the terms of the GNU Lesser General Public <BR> * License as published by the Free Software Foundation; either <BR> * version 2.1 of the License, or (at your option) any later version. <BR> * <BR> * This library is distributed in the hope that it will be useful, <BR> * but WITHOUT ANY WARRANTY; without even the implied warranty of <BR> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU <BR> * Lesser General Public License for more details. <BR> * <BR> * You should have received a copy of the GNU Lesser General Public <BR> * License along with this library; if not, write to the Free Software <BR> * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA <BR> * <BR> * Contact information: <BR> * Dao Gottwald <dao at design-noir.de> <BR> * Herltestra?e 12 <BR> * D-01307, Germany <BR> * <BR> * @version 1.5 <BR> * @url http://design-noir.de/webdev/JS/loadScript/ <BR> */ <br><br>function loadScript (url, callback) { <BR> var script = document.createElement('script'); <BR> script.type = 'text/javascript'; <BR> /* should be application/javascript <BR> * http://www.rfc-editor.org/rfc/rfc4329.txt <BR> * http://connect.microsoft.com/IE/feedback/ViewFeedback.aspx?FeedbackID=84613 <BR> */ <BR> if (callback) <BR> script.onload = script.onreadystatechange = function() { <BR> if (script.readyState && script.readyState != 'loaded' && script.readyState != 'complete') <BR> return; <BR> script.onreadystatechange = script.onload = null; <BR> callback(); <BR> }; <BR> script.src = url; <BR> document.getElementsByTagName('head')[0].appendChild (script); <BR>} <BR></script>
 

实例:
复制代码 代码如下:
<script> <BR>// prevent google analytics from slowing down page loading <BR>window.addEventListener ('load', function() { <BR> loadScript ('http://www.google-analytics.com/urchin.js', function() { <BR> window._uacct = 'UA-xxxxxx-x'; <BR> urchinTracker(); <BR> }); <BR>}, false); <BR></script>
Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn