首页 > 建站‧营销

判断当前页面所在的域名加载不同的js


判断当前页面所在的域名加载不同的js,代码如下:

<script type="text/javascript">
if(location.host.indexOf("aaa.com")!=-1)
document.write('<script type="text/javascript" src="a.js"></scr'+'ipt>');	
if(location.host.indexOf("bbb.com")!=-1)
document.write('<script type="text/javascript" src="b.js"></scr'+'ipt>');	
</script>

这段代码的作用是判断当前页面所在的域名,如果是在aaa.com域名下,就加载a.js文件;如果是在bbb.com域名下,就加载b.js文件。其中使用了document.write()方法来动态添加<script>标签,实现文件的加载。

本文链接:https://www.zhanque.net/cms/3292.html