用Javascript加载脚本-不';你不能在铬上工作吗?
我正在尝试用Javascript加载以下脚本:用Javascript加载脚本-不';你不能在铬上工作吗?,javascript,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
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;
}
}
函数put(){
var组=document.getElementById(“obj_0123456790”);
var children=group.childNodes;
对于(var i=0;i
我用以下代码加载它:
<script>
var i=document.createElement('script');
i.src="http://website.com/putter.js";
document.head.appendChild(i);
</script>
var i=document.createElement('script');
i、 src=”http://website.com/putter.js";
文件.标题.附件(i);
它在firefox上运行良好,但在chrome上不起作用。如何使其在chrome上工作?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.如果页面上已经有jQuery,只需使用
最简单的解决方案是将所有脚本内联在页面底部,这样它们在执行时不会阻止HTML内容的加载。它还避免了必须异步加载每个所需脚本的问题
如果您有一个特别奇特的交互,但并不总是使用它,它需要某种更大的脚本,那么在需要之前避免加载该特定脚本(延迟加载)可能会很有用
3.谷歌的例子
<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>
你说的“它不起作用”到底是什么意思?如果你不动态添加script元素,它会起作用吗?@Jason有很多库可以帮助你执行这样的资源加载,这样你就可以在所有浏览器中顺利完成!只需查看HeadJS:
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'
}
]);