欢迎来到代码驿站!

JavaScript代码

当前位置:首页 > 网页前端 > JavaScript代码

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

时间:2021-03-09 10:08:27|栏目:JavaScript代码|点击:
复制代码 代码如下:

<script type="text/javascript"> 
/** 
 * function loadScript 
 * Copyright (C) 2006 Dao Gottwald 
 * 
 * This library is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU Lesser General Public 
 * License as published by the Free Software Foundation; either 
 * version 2.1 of the License, or (at your option) any later version. 
 * 
 * This library is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
 * Lesser General Public License for more details. 
 * 
 * You should have received a copy of the GNU Lesser General Public 
 * License along with this library; if not, write to the Free Software 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 
 * 
 * Contact information: 
 * Dao Gottwald <dao at design-noir.de> 
 * Herltestra?e 12 
 * D-01307, Germany 
 * 
 * @version 1.5 
 * @url http://design-noir.de/webdev/JS/loadScript/ 
 */ 

function loadScript (url, callback) { 
  var script = document.createElement('script'); 
  script.type = 'text/javascript'; 
  /* should be application/javascript 
   * http://www.rfc-editor.org/rfc/rfc4329.txt 
   * http://connect.microsoft.com/IE/feedback/ViewFeedback.aspx?FeedbackID=84613 
   */ 
  if (callback) 
    script.onload = script.onreadystatechange = function() { 
      if (script.readyState && script.readyState != 'loaded' && script.readyState != 'complete') 
        return; 
      script.onreadystatechange = script.onload = null; 
      callback(); 
    }; 
  script.src = url; 
  document.getElementsByTagName('head')[0].appendChild (script); 

</script>
 

实例:
复制代码 代码如下:
<script type="text/javascript">
// prevent google analytics from slowing down page loading
window.addEventListener ('load', function() {
  loadScript ('http://www.google-analytics.com/urchin.js', function() {
    window._uacct = 'UA-xxxxxx-x';
    urchinTracker();
  });
}, false);
</script>

上一篇:高性能Javascript笔记 数据的存储与访问性能优化

栏    目:JavaScript代码

下一篇:js编码、解码函数介绍及其使用示例

本文标题:Code:loadScript( )加载js的功能函数

本文地址:http://www.codeinn.net/misctech/77375.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有