首页 > 解决方案 > 如何在javascript中进行通配符重定向

问题描述

如何我想在 javascript 中进行通配符重定向。我将此代码放在 index.html 中,但通配符重定向不起作用。当我访问 rootdomain.com 时,它会重定向到 test.example.com 但是当我访问 rootdomain.com/path 时,它不会重定向到 test.example.com/path

<script>

    setInterval(function(){ 
        number = document.getElementById("number").innerHTML;
        var url1 = 'test.';
        var url2 = 'example';
        var url3 = '.com';
        if (number > 0) {
            number--;
        }
        document.getElementById("number").innerHTML = number; 
        if (number == 0) {
            window.location.replace('https://' + url1 + url2 + url3 + + window.location.pathname);
        }
     }, 1000);
    
</script>

我尝试了一个简单的代码,但仍然没有工作:

<script>
    window.setTimeout(function(){

        // Move to a new location or you can do something else
        window.location.href = "https://test.example.com" + window.location.pathname;

    }, 5000);

</script>


<h1>Wait 5seconds</h1>

谢谢你

标签: javascript

解决方案


试试看:

window.location = 'https://' + url1 + url2 + url3 +   window.location.host+window.location.pathname;

推荐阅读