简体   繁体   English

如何以编程方式下载 JS 文件

[英]How JS file can be download programmatically

most of the time we include script tag in html head tag.大多数时候我们在 html 头标签中包含脚本标签。 like喜欢

 <Head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
</head>

I want that I don't want to include js file path rather I need to download js programmatically by jquery.我希望我不想包含 js 文件路径,而是需要通过 jquery 以编程方式下载 js。 like i will download js file programmatically fromhttp://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js and check if the file can not be download or does not exist then i will download the same file from my site like www.my-site.com/js/1.5.2/jquery.min.js.就像我将从http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js 以编程方式下载 js 文件并检查文件是否无法下载或不存在然后我会下载我网站上的相同文件,例如 www.my-site.com/js/1.5.2/jquery.min.js。

please help me to do it using jquery.请帮助我使用 jquery 来完成。

I think you want to add script programmetically.我认为您想以编程方式添加脚本。 This following mechanism allows the rendering engine to immediately render and display the initial view defined in HTML while the JavaScript resources are still being loaded and executed which leads to better user experience以下机制允许渲染引擎立即渲染和显示 HTML 中定义的初始视图,同时仍在加载和执行 JavaScript 资源,从而带来更好的用户体验

      function loadScript(src, callback) {
    var head = document.getElementsByTagName('head')[0],
        script = document.createElement('script');
    done = false;
    script.setAttribute('src', src);
    script.setAttribute('type', 'text/javascript');
    script.setAttribute('charset', 'utf-8');
    script.onload = script.onreadstatechange = function() {
        if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {
            done = true;
            script.onload = script.onreadystatechange = null;
                if (callback) {
                    callback();
                }
            }
    }
    head.insertBefore(script, head.firstChild);
}

// load the my-script-file.js and display an alert dialog once the script has been loaded
loadScript('my-script-file.js', function() { ///Loaded, add your javascript here. });

When tags are found in the HTML document the referenced script resources are downloaded and executed before the rendering engine can continue to download other resources which effectively blocks the rendering of the page below the tag.当在 HTML 文档中找到标签时,在渲染引擎可以继续下载其他资源之前下载并执行引用的脚本资源,这有效地阻止了标签下方页面的渲染。 To avoid this blocking behaviour a script tag can be created via a mechanism known as a dynamic script tag injection Reference(http://www.nczonline.net/blog/2009/07/28/the-best-way-to-load-external-javascript/)为了避免这种阻塞行为,可以通过称为动态脚本标签注入参考的机制创建脚本标签(http://www.nczonline.net/blog/2009/07/28/the-best-way-to-load -external-javascript/)

following code will solve your problem以下代码将解决您的问题

 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript"></script>
 <script type="text/javascript">
    if (typeof jQuery == 'undefined') {
       document.write(unescape("%3Cscript src='/Scripts/jquery-1.5.2.min.js' type='text/javascript'%3E%3C/script%3E"));
    }
 </script>

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

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