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

2016-02-19 10:50 14 1 收藏

今天天气好晴朗处处好风光,好天气好开始,图老师又来和大家分享啦。下面给大家推荐Code:loadScript( )加载js的功能函数,希望大家看完后也有个好心情,快快行动吧!

【 tulaoshi.com - Web开发 】

代码如下:

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

来源:https://www.tulaoshi.com/n/20160219/1595905.html

延伸阅读
标签: Web开发
习惯了用java,在java中,需要某个类直接import就可以了,所以做javascript的时候也想实现这个效果。 前段时间,用了下dojo,里面的dojo.require的功能很不错,一看代码,晕了,一大堆东西,唉~还是自己写个简单点的,dojo.require可以引入包,我就只做一个导入js文件的。 开始用的document.write,执行顺序不对了,这是在后面进行导...
标签: Web开发
函数名称 函数功能 Cbool(string) 转换为布尔值 Cbyte(string) 转换为字节类型的值 Ccur(string) 转换为货币类值 Cdate(string) 转换为日前类型的值 Cdbl(string) 转换为双精度值 Cint(string) 转换为整数值 Clng(string) 转换为长整型的值 Csng(string) 转换为单精度的值 Cstr(var) 转换为字符串值 Str(var) 数值转换为字符串 Val(string) 字符...
标签: Web开发
原理解析:第一步:使用dom创建script或者link标签,并给他们附加属性,如type等第二步:使用appendChild方法把标签绑定到另一个标签,一般是绑到head. 应用:1、提高代码的复用,减少代码量;2、添加一个javascript控制器和 session可以实现动态改变页面样式;3、由于是页面是从上到下依次加载文件的,并且边加载边解释,所以可以添加jav...
标签: Web开发
代码如下: var Rash=true; var msg=""; function norash() { if (confirm("确定要取消吗")) Rash=false; }  function rashit() { setInterval('getrss()',Inttime); } function getrss() {         if (Rash==true)    &nb...
标签: Web开发
前段时间在用google map api的函数库的时候,发现里面的downloadUrl函数非常好用,所以自己写了一个。用腻了那些什么框架什么池,到头来发现越简单的东西越是适合我这种懒人。 downloadUrl(url, callback, data); 参数说明: url不用说了; callback是回调函数,函数调用的时候会有两个参数:data, responseCode...

经验教程

64

收藏

66
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部