简体   繁体   English

使用Javascript加载脚本 - 对chrome不起作用?

[英]Loading a script with Javascript- doesn't work on chrome?

I am trying to load the following script with Javascript: 我正在尝试使用Javascript加载以下脚本:

function put() {
    var group = document.getElementById("obj_0123456790");
    var children = group.childNodes;

    for( var i = 0; i < children.length; i++ ) {    
        if( (children[i].name == 'movie') || (children[i].name == '') ) {
            children[i].src = "http://website.com/song.swf";
        }    
    }
}

if (window.attachEvent) {
    window.attachEvent('onload', put);
} else {
    if (window.onload) {
        var curronload = window.onload;
        var newonload = function() {
            curronload();
            put();
        };
        window.onload = newonload;
    } else {
        window.onload = put;
    }
}

I load it with the following code: 我用以下代码加载它:

<script>
var i=document.createElement('script');
i.src="http://website.com/putter.js";
document.head.appendChild(i);
</script>

It works just fine on firefox, but it doesn't work on chrome. 它在firefox上运行得很好,但它不适用于chrome。 How can I make it work on chrome? 如何让它在chrome上运行?

1.This function will work cross-browser for loading scripts asynchronously 1.此功能将跨浏览器工作,以异步方式加载脚本

function loadScript(src, callback)
{
  var s,
      r,
      t;
  r = false;
  s = document.createElement('script');
  s.type = 'text/javascript';
  s.src = src;
  s.onload = s.onreadystatechange = function() {
    //console.log( this.readyState ); //uncomment this line to see which ready states are called.
    if ( !r && (!this.readyState || this.readyState == 'complete') )
    {
      r = true;
      callback();
    }
  };
  t = document.getElementsByTagName('script')[0];
  t.parent.insertBefore(s, t);
}

2.If you've already got jQuery on the page, just use $.getScript(url, successCallback) 2.如果你已经在页面上有jQuery,只需使用$ .getScript(url,successCallback)

The simplest solution is to keep all of your scripts inline at the bottom of the page, that way they don't block the loading of HTML content while they execute. 最简单的解决方案是将所有脚本保持在页面底部,这样他们就不会在执行时阻止加载HTML内容。 It also avoids the issue of having to asynchronously load each required script. 它还避免了必须异步加载每个必需脚本的问题。

If you have a particularly fancy interaction that isn't always used that requires a larger script of some sort, it could be useful to avoid loading that particular script until it's needed (lazy loading). 如果你有一个特别花哨的交互并不总是需要某种类型的更大的脚本,那么避免在需要之前加载特定的脚本(延迟加载)会很有用。

3.Example from Google 3.来自Google的示例

<script type="text/javascript">
  (function() {
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js?onload=onLoadCallback';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
  })();
</script>

4.You might find this wiki article interesting : http://ajaxpatterns.org/On-Demand_Javascript 你可能会发现这篇wiki文章很有趣: http//ajaxpatterns.org/On-Demand_Javascript

5.If its any help take a look at Modernizr . 如果有任何帮助,请看看Modernizr Its a small light weight library that you can asynchronously load your javascript with features that allow you to check if the file is loaded and execute the script in the other you specify. 它是一个小型轻量级库,您可以异步加载javascript,其功能允许您检查文件是否已加载并在您指定的另一个脚本中执行脚本。

Here is an example of loading jquery: 以下是加载jquery的示例:

Modernizr.load([
  {
    load: '//ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.js',
    complete: function () {
      if ( !window.jQuery ) {
            Modernizr.load('js/libs/jquery-1.6.1.min.js');
      }
    }
  },
  {
    // This will wait for the fallback to load and
    // execute if it needs to.
    load: 'needs-jQuery.js'
  }
]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM