动态加载Java脚本文件 [英] Dynamically loading Java Script files

查看:92
本文介绍了动态加载Java脚本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以在调用"$(document).ready"之前动态加载一些JS文件.这些JS文件应该已加载,并且可以在ready事件处理程序中使用.

I would like to know if there is a way to dynamically load some JS files before "$(document).ready" gets called. These JS files should be loaded and available in the ready event handler.

jquery提供了一种方法吗?

Does jquery provide a way to do this?

这里的问题(如您所料)是根据选择的语言环境/语言来加载我的JS文件的特定本地化版本的能力.

The issue here (as you might expect) is the ability to load a specific localized version of my JS files depending on whichever locale/language is selected.

谢谢

推荐答案

如果您要使用纯JavaScript,可以尝试一下.

If you want in pure javascript you can try this.

   var head= document.getElementsByTagName('head')[0];
   var script= document.createElement('script');
   script.type= 'text/javascript';
   script.onreadystatechange= function () {
      if (this.readyState == 'complete'){
          //Your can write your code here
      };
   }
   script.src= 'script.js';
   head.appendChild(script);

或者,您可以使用jQuery的getScript方法

Alertnatively you can use jQuery's getScript method

$.getScript("script.js", function(){
    //Your can write your code here
});

这篇关于动态加载Java脚本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆