gpt4 book ai didi

javascript - 运行 JavaScript 代码可以加载 JSONP 而不是脚本标签吗?

转载 作者:行者123 更新时间:2023-11-28 02:26:12 25 4
gpt4 key购买 nike

我刚刚在 this great question 的答案的帮助下让 JSONP 工作起来。现在,在我的设置中,HTML 文件通过 script 标记加载 JavaScript:

<script src="js/playlistcontroller.js"></script>

现在我的问题是,这段代码在运行时能否触发加载我的 JSONP 响应?

目标是避免拥有

<script src="playlistcontent.jsonp"></script>

在 HTML Head 中,但在 Controller 中执行这些操作(如果您好奇的话,它恰好是 AngularJS Controller )。

这可能吗(从 JavaScript 加载 JavaScript)?

最佳答案

完全跨浏览器测试的脚本:

var CFLoad = {
fScript : null,
isFileReady : function ( v ) {
return ( ! v || v == "loaded" || v == "complete" || v == "uninitialized" );
},
js : function(src,cb,attrs) {
var s = document.createElement( "script" ),
done = !1, i;
s.src = src;
s.type = "text/javascript";
for ( i in attrs ) {
s.setAttribute( i, attrs[ i ] );
}
s.onreadystatechange = s.onload = function () {
if ( ! done && CFLoad.isFileReady( s.readyState ) ) {
done = !0;
if(cb) cb(s);
s.onload = s.onreadystatechange = null;
}
};
window.setTimeout(function() {
if( !done) {
done = !0;
if(cb) cb(s,1);
}
}, 5000);
if(this.fScript===null) this.init();
this.fScript.parentNode.insertBefore( s, this.fScript );
},
css : function(href,cb,attrs) {
var l = document.createElement("link"),i;
l.href = href;
l.rel = "stylesheet";
l.type = "text/css";
for ( i in attrs ) {
l.setAttribute( i, attrs[i]);
}
if(this.fScript===null) this.init();
this.fScript.parentNode.insertBefore(l,this.fScript);
if(cb) window.setTimeout(cb, 0);
},
init : function() {
this.fScript = document.getElementsByTagName( "script" )[ 0 ];
}
};

使用

CFLoad.js("http://code.jquery.com/jquery-1.8.3.js", function (script_tag, failed) {
if(!failed) {
CFLoad.js("http://code.jquery.com/ui/1.9.2/jquery-ui.js", function(s, f) {
if(!f) {
alert("Loaded");
js13 = jQuery.noConflict(true);
main();
}
})
}
});

就您而言:

CFLoad.js("playlistcontent.jsonp");

关于javascript - 运行 JavaScript 代码可以加载 JSONP 而不是脚本标签吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14902217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com