有时候js或css需要后期加载,不需要页面载入的时候加载,这时候客户通过下面方式限定一下访问css及js的方法变量
<script>
var dynamicLoading = {
css: function (path) {
if (!path || path.length === 0) {
throw new Error('argument "path" is required !');
}
var head = document.getElementsByTagName('head')[0];
var link = document.createElement('link');
link.href = path;
link.rel = 'stylesheet';
link.type = 'text/css';
head.appendChild(link);
},
js: function (path) {
if (!path || path.length === 0) {
throw new Error('argument "path" is required !');
}
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.src = path;
script.type = 'text/javascript';
head.appendChild(script);
}
}
</script>
然后在需要载入调用的时候,通过下面的方式载入
dynamicLoading.js("/Content/showImage/js/jquery.min.js");
dynamicLoading.js("/Content/showImage/js/jquery.flexslider.js");
dynamicLoading.js("/Content/showImage/js/effects.js");
网友评论