首頁  >  文章  >  web前端  >  Code:loadScript( )加载js的功能函数_javascript技巧

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

WBOY
WBOY原創
2016-05-16 19:19:461227瀏覽
复制代码 代码如下:

<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>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn