简体   繁体   English

重定向到同一网站,但链接不同(javascript)

[英]redirect to same site, but different link (javascript)

I want that people in "example.com/index.htm" get redirected to "example.com". 我希望“ example.com/index.htm”中的人被重定向到“ example.com”。 but the code is being tricky. 但是代码很棘手。 It always takes me to a loop. 它总是把我带到一个循环。

<script type="text/javascript">
function check()
{
 if (window.top!="example.com") 
 {
  window.location="http://example.com";
 }else {} 
}</script>
<script type="text/javascript">
function check()
{
if (window.top.location.href!="http://mysite.com") 
 {
window.top.location.href="http://mysite.com";
}else {} 
}</script>

You need to use the location object and it needs to actually match. 您需要使用位置对象,并且它需要实际匹配。

Give this a go (untested): 尝试一下(未试用):

$(function() {

    var reg = /index.htm/g,
        top = window.location.href,
        loc = '';

    if(reg.test(top)) {

        top = top.replace('http://','');
        top = top.split('/');

        for(i=0;i<top.length-1;i++) {
            loc = loc + top[i];
        }

        window.location.href = 'http://' + loc;
    }

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM