使用按钮在页面中切换js src

时间:2014-05-20 04:20:53

标签: javascript html

我可以在网页上使用javascript执行此操作吗?

<head>
    <script src="trans_func.js" type="text/javascript"></script>
    <script src="language/English.js" type="text/javascript"></script>
    <script src="language/Chinese.js" type="text/javascript"></script>

</head>

由于它是英文或中文,我可以在点击按钮时注释掉一行代码吗?当我再次点击它时会注释另一行吗?

1 个答案:

答案 0 :(得分:1)

单击按钮,您可以根据条件加载js。

if(condition!="English") // lang can also be variable which is set from the backend
    {
        document.write('<script src="language/Chinese.js"></script>');
    }
else
    {
        document.write('<script src="language/English.js"></script>');
    }

如果它基于lanauge:

var userLang = navigator.language || navigator.userLanguage; 
if(userLang!='en-us') // lang can also be variable which is set from the backend
        {
            document.write('<script src="language/Chinese.js"></script>');
        }
    else
        {
            document.write('<script src="language/English.js"></script>');
        }