重定向到新网址

时间:2014-12-14 19:40:42

标签: javascript jquery html

任何人都可以提供关于某个页面是否重定向到新页面的示例

例如。

如果在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');

当点击链接“Women Shoes”(id =#cli68)时也需要这个工作(通常它会转到第一个网址但是想要在点击时转到第二个网址以及第一个链接页面时。

2 个答案:

答案 0 :(得分:0)

您可以将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
    }  
});

如果您难以在您的aspx代码中添加javascript,可以使用以下链接

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!=因为页面的网址不同所以要测试此代码,您需要将其复制到一个emmpty html doc中。 如果要将其集成到代码中,请使用document.URL==

希望有所帮助,

答案 1 :(得分:0)

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

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

另一种选择是在页面加载上运行一个函数,它只会改变你想要的链接上的href到新的销售链接。我认为这个选项更好,因为一切都按照用户的期望行事。

相关问题