重定向到新网址

Redirecting to a new url

本文关键字:新网址 重定向      更新时间:2023-09-26

任何人都可以提供一个例子,说明是否在某个页面上重定向到新页面

例如。

如果在www.site.com/womens/shoes/all.aspx,那么他们将被重定向到www.site.com/womens/shoes/sale/all.aspx

我会在下面使用类似这段代码的东西吗?

window.location.replace('www.site.com/womens/shoes/sale/all.aspx');

当单击链接"女鞋"(id=#cli68)时也需要它才能工作(通常它会转到第一个URL,但在单击时和在第一个链接页面上时想要转到第二个URL。

您可以将 jquery 与 $document.ready() 函数一起使用

添加对jquery的支持并将其添加到您的javascript代码中

//executed when doc is ready
$( document ).ready(function() {
    if (document.URL=='www.site.com/womens/shoes/all.aspx'){//checks current url location
        window.location.replace("www.site.com/womens/shoes/sale/all.aspx");//redirect it
    }  
});

如果您难以将javascript添加到aspx代码中,则可以使用以下链接

http://msdn.microsoft.com/en-us/library/aa479011.aspx

这是一个工作示例

<html>
<head>
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script>
    $( document ).ready(function() {
        console.log( "document loaded" );
        if (document.URL!='http://shop.mischiefshoes.co.nz/estore/category/women%27s/christmas%20styles.aspx'){//checks current url location
            console.log( "redirect" );
            window.location.replace("http://shop.mischiefshoes.co.nz/estore/stylelist/all.aspx?page=1&rsb=%2C0%7C3%7C14%7CRed%2C0%7C3%7C48%7CRed%2C0%7C3%7C68%7CRed%2C0%7C3%7C77%7CRed%2C0%7C3%7C78%7CRed%2C0%7C3%7C90%7CRed%2C0%7C3%7C92%7CRed%2C0%7C3%7C102%7CDark%20Red%2C0%7C3%7C108%7CRed%2C0%7C3%7C113%7CRed%2C0%7C3%7C120%7CRed%2C0%7C3%7C127%7CRed%2C0%7C3%7C129%7CBlack%2C0%7C3%7C140%7CRed%2C0%7C3%7C144%7CRed%2C0%7C3%7C164%7CRed%2C0%7C3%7C174%7CRed%2C0%7C3%7C177%7CRed%2C0%7C3%7C181%7CRed%2C0%7C3%7C191%7CRed%2C0%7C3%7C212%7CRed%2C0%7C3%7C224%7CRed%2C0%7C3%7C225%7CRed%2C0%7C3%7C246%7CRed%20Multi%2C0%7C3%7C261%7CRed%2C0%7C3%7C266%7CRed%20Multi%2C0%7C3%7C267%7CRed%2C0%7C3%7C280%7CRed%2C0%7C3%7C293%7CWhite%20Multi%2C0%7C3%7C301%7CRed%2C0%7C3%7C303%7CRed%2C0%7C3%7C328%7CRed%2C0%7C3%7C350%7CRed%2C0%7C3%7C362%7CRed%2C0%7C3%7C417%7CNavy%20Red%20White%2C0%7C3%7C427%7CRed%20White%20Navy%2C0%7C3%7C440%7CRed%2C0%7C3%7C481%7CFuschia%2C0%7C3%7C517%7CPurple%2C0%7C3%7C518%7CBlack%2C0%7C3%7C539%7CBlack%20White%2C0%7C3%7C544%7CRed%2C0%7C3%7C559%7CPewter%2C0%7C3%7C606%7CRed%2C0%7C3%7C627%7CRed%2C0%7C3%7C634%7CRed%2C0%7C3%7C638%7CRed%2C0%7C3%7C641%7CRed%2C0%7C3%7C650%7CRed%2C0%7C3%7C660%7CRed%2C0%7C3%7C668%7CFuschia%2C0%7C3%7C671%7CRed%2C0%7C3%7C695%7CRed%2C0%7C3%7C704%7Cred%2C0%7C3%7C719%7CRed%2C0%7C3%7C727%7CRed%2C0%7C3%7C741%7CNavy%20Yellow%20Red%2C0%7C3%7C765%7CRed%2C0%7C3%7C779%7CRed%20Suede%2C0%7C3%7C847%7CFluro%20Red%2C0%7C3%7C860%7CRed%20Sqiggle%2C0%7C3%7C870%7CNavy%2FRed%2C0%7C3%7C877%7CRed%20Squiggle%2C0%7C3%7C880%7CRed%20Squig%2C0%7C3%7C909%7CRed%2C0%7C3%7C924%7CRed%2FFuschia%2C0%7C3%7C934%7CTan%2FRed%2C0%7C3%7C972%7Cred%2C0%7C3%7C997%7CRed%20Pony%2C0%7C3%7C1001%7CRed%2FEggplant%2C0%7C3%7C1008%7CRed%2FSlate%2C0%7C3%7C1058%7CWhite%2FRed%2FGrey%2C0%7C3%7C1068%7CRed%2FBeige%20%2C0%7C3%7C1151%7CRed%20Tartan%2C0%7C3%7C1237%7Cnavy%2Fred%2Fwhite%2C0%7C3%7C1263%7CRed%2C0%7C3%7C1277%7CWht%2Fblk%2Fred%2C0%7C3%7C1282%7Cred%2C0%7C3%7C1303%7CRed%20Snake%2C0%7C3%7C1308%7Cred&rsc=%2C0%2C");//redirect it
        }         
    });
    $( window ).load(function() {
        console.log( "window loaded" );
    });
    </script>
</head>
<body>
    The content of the document......
</body>
</html>

我使用了document.URL!=因为页面的 url 不同,因此要测试此代码,您需要将其复制到 emmpty html 文档中。如果要将其集成到代码中,请使用document.URL==

希望有帮助,

您可以为链接附加一个 onclick 事件,然后使用

window.location = "www.site.com/womens/shoes/sale/all.aspx";

另一种选择是在页面加载时运行一个函数,该功能只会将您想要的链接上的 HREF 更改为新的销售链接。我认为此选项更好,因为一切都按照用户期望的方式运行。