日期:2014-05-16  浏览次数:20529 次

简单问题:动态加载js
现在有个这个需求:

a.htm 页面要根据 不同语言选择来加载不同js文件。

那么这个语句怎么写呢?

比如:

JScript code

if( language == english)
{
//只加载en.js
}else( language == Traditional Chinese)
{
//只加载trach.js
}else( language == simple chinese)
{
//只加载sipch.js
]



------解决方案--------------------
HTML code

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
    <title>new document</title>
    <script type="text/javascript">
    function loadScript(url){
        document.write("<scr" + "ipt src='" + url + "' ></scr" + "ipt>");
    }
    if(language == english)
    {
        //只加载en.js
        loadScript("en.js");
    }else( language == Traditional Chinese)
    {
        //只加载trach.js
        loadScript("trach.js");
    }else( language == simple chinese)
    {
        //只加载sipch.js
        loadScript("sipch.js");
    }
    </script>
</head>
<body>

</body>
</html>

------解决方案--------------------
JScript code
var head= document.getElementsByTagName('head')[0];
var script=document.createElement("script");
script.type= 'text/javascript';
if( language == english)
{script.src="en.js";
//只加载en.js
}else( language == Traditional Chinese)
{script.src="en.js";
//只加载trach.js
}else( language == simple chinese)
{script.src="en.js";
//只加载sipch.js
}
head.appendChild(script);

------解决方案--------------------
探讨
JScript code
var head= document.getElementsByTagName('head')[0];
var script=document.createElement("script");
script.type= 'text/javascript';
if( language == english)
{script.src="en.js";
//只加载e……

------解决方案--------------------
JScript code
<script type="text/javascript" id="language" src=""></script> <script type="text/javascript">  if (language == english) { language.src = "en.js"; } else if (language == "chinese") { language.src = "ch.js"; } </script>

------解决方案--------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>new document</title>
<script type="text/javascript">
function loadScript(url){
document.write("<scr" + "ipt src='" + url + "' ></scr" + "ipt>");
}
if(language == english)
{
//只加载en.js
<%@ include file="en.js"%>
}else( language == Traditional Chinese)
{
//只加载trach.js
<%@ include file="trach.js"%>
}else( language == simple chinese)
{
//只加载sipch.js
<%@ include file="sipch.js"%> }
</script>
</head>
<body>

</body>
</html>

------解决方案--------------------
jquery 

$.getScript("123.js")