我编写了一个JavaScript文件,该文件仅对于使用Firefox的用户是必需的,因此我不希望其他用户甚至加载它。
ComboBox2
是否可以修改此标记,使其仅在Firefox上可用?
答案 0 :(得分:3)
要获取浏览器名称,可以使用:
navigator.userAgent.toLowerCase();
要加载另一个脚本,可以使用jquery方式:
$.getScript("another_script.js");
这里有一个带有示例的代码段(已通过Firefox和chrome测试)。
another_script.js
$(function() {
alert("loaded");
});
main.html
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
var bwsr = navigator.userAgent.toLowerCase();
console.log(bwsr);
$(function() {
if (bwsr.startsWith("mozilla")) {
console.log("firefox");
$.getScript("another_script.js");
} else {
console.log("not firefox, nothing will be done here");
}
});
/*
*/
</script>
</head>
<body>
</body>
</html>