-1){ //跳转 location.href = "http://baidu.com";" />
牛骨文教育服务平台(让学习变的简单)
博文笔记

如何利用JS判断当前来路域名并跳转到指定页面

创建时间:2017-09-11 投稿人: 浏览次数:1573

1.如何利用JS判断当前来路域名并跳转到指定页面

获取当前请求路径
var href = location.href ;
if(href.indexOf("baidu")>-1){
//跳转

location.href = "http://baidu.com";
}

2.

今天有一客户要求他的网站输入两个不同的域名后需要载入不同网页头部。思前想来,也只有通过JS的判断来加载不同的CSS。

代码如下:
host = window.location.host;
if (host=="lezi.66ghz.com") 

document.write("<style>")
document.write(".head{ background: url(document.write("</style>")
}

3.不同的域名   输出不同的结果

<script type="text/javascript">
        if (window.location.href.indexOf("www.xt.com") > -1) {
        document.write("<li style="background:none;"><a href="http://www.xt.com/yygk/yyjj/915.html">介绍</a></li>");
        } else if (window.location.href.indexOf("lezi.66ghz.com") > -1) {
        document.write("<li style="background:none;"><a href="http://www.gzxt.com/yygk/yyjj/916.html">介绍</a></li>");
        } else if (window.location.href.indexOf("www.gzxt.com") > -1) {
        document.write("<li style="background:none;"><a href="http://www.gzxt.com/yygk/yyjj/917.html">介绍</a></li>");
        }
 </script>

其实我还是觉得直接用js来操作比较简单粗暴一点,吧上面这段一改就成自己的了,把下面这个文件往根目录一丢,万事大吉,哈哈哈。。。

<!DOCTYPE html>
<html>
<head>
	<title>正在玩命加载</title>
	<!-- 判断来路域名自动跳转 -->
	<script type="text/javascript">
        if (window.location.href.indexOf("lezi.66ghz.com") > -1) {
        	window.location.href="http://www.thevv.top/dd/";
        } else if (window.location.href.indexOf("wx.thevv.top") > -1) {
        	window.location.href="http://www.thevv.top/wx/";
        } else if (window.location.href.indexOf("www.bookhai.top") > -1) {
        	window.location.href="http://www.thevv.top/book/";
        }
        </script>
</head>
<body>

</body>
</html>
 
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。