Javascript Mvc完全异步脚本加载
我在网页上异步加载脚本时遇到了一个小问题。 我需要异步加载页面的所有脚本。我尝试了很多程序,我在谷歌上找到了这些程序,但仍然不完美 现在我有这样一个:Javascript Mvc完全异步脚本加载,javascript,c#,asp.net-mvc,Javascript,C#,Asp.net Mvc,我在网页上异步加载脚本时遇到了一个小问题。 我需要异步加载页面的所有脚本。我尝试了很多程序,我在谷歌上找到了这些程序,但仍然不完美 现在我有这样一个: function async(u, c) { var d = document, t = 'script', o = d.createElement(t), s = d.getElementsByTagName(t)[0]; o.src = u; if (c) { o.addEven
function async(u, c) {
var d = document, t = 'script',
o = d.createElement(t),
s = d.getElementsByTagName(t)[0];
o.src = u;
if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
s.parentNode.appendChild(o, s);
}
async("/bundles/jquery", function() {
//here, load scripts from inner pages. Index, Detail...
});
@Scripts.RenderFormat("<script async type=\"text/javascript\" src=\"{0}\"></script>", "~/bundles/raphael")
...
@Scripts.RenderFormat("<script async type=\"text/javascript\" src=\"{0}\"></script>", "~/bundles/frontall")
...
@RenderSection("scripts", required: false)
这里我们得到错误“$未定义”,因为jquery尚未加载
在内容之后,在布局文件中我有:
@Scripts.RenderFormat("<script async type=\"text/javascript\" src=\"{0}\"></script>", "~/bundles/raphael")
...
@Scripts.RenderFormat("<script async type=\"text/javascript\" src=\"{0}\"></script>", "~/bundles/frontall")
...
@RenderSection("scripts", required: false)
有没有办法,怎么解决?
非常感谢您抽出时间。好了,伙计们,问题解决了
布局中有我当前的纸条:
...
@RenderSection("scripts", required: false)
<script>
function async(u, c, css) {
var t = 'script';
var element = document.createElement(t);
var s = document.getElementsByTagName(t)[0];
element.src = u;
if (css) {
t = "link";
element = document.createElement(t);
element.href = u;
element.rel = 'stylesheet';
element.src = null;
var head = document.getElementsByTagName('head')[0];
head.appendChild(element, head);
return;
}
if (c) { element.addEventListener('load', function (e) { c(null, e); }, false); }
s.parentNode.appendChild(element, s);
}
function initScripts() {
async("/content/css", null, true);
async("/bundles/jquery", function () {
@RenderSection("asyncScripts", required: false)
});
}
if (window.addEventListener) {
window.addEventListener("load", function () {
initScripts();
}, false);
}
else if (window.attachEvent) {
window.attachEvent("onload", function () {
initScripts();
});
} else {
window.onload = function () {
initScripts();
};
}
</script>
。。。
@RenderSection(“脚本”,必需:false)
函数异步(u、c、css){
var t='script';
var元素=document.createElement(t);
var s=document.getElementsByTagName(t)[0];
element.src=u;
如果(css){
t=“链接”;
元素=document.createElement(t);
element.href=u;
element.rel='stylesheet';
element.src=null;
var head=document.getElementsByTagName('head')[0];
head.appendChild(元素,head);
返回;
}
if(c){element.addEventListener('load',函数(e){c(null,e);},false);}
s、 parentNode.appendChild(元素,s);
}
函数initScripts(){
异步(“/content/css”,null,true);
异步(“/bundles/jquery”,函数(){
@RenderSection(“异步脚本”,必需:false)
});
}
if(window.addEventListener){
addEventListener(“加载”,函数(){
initScripts();
},假);
}
else if(窗口附件){
attachEvent(“onload”,函数(){
initScripts();
});
}否则{
window.onload=函数(){
initScripts();
};
}
在其他文件中。(索引、细节…)
@节脚本{
捷克共和国={};
window.selectedRegions=[];
捷克共和国地区={
@Html.Raw(string.Join(“,”,regions.Select(r=>string.Format(“{0}\”:“{1}\”,r.RaphaelId,r.RaphaelCoordinates)))
};
@节异步脚本{
@Scripts.RenderFormat(“async(\“{0}\”);“,“~/bundles/raphael”);
}
如果需要调用下一个脚本,例如在raphael之后,只需在renderformat async a中创建一个新的回调即可
多谢大家