从页面重定向中排除id(jquery)

时间:2014-12-02 22:18:57

标签: javascript jquery redirect

我刚刚在stackoverflow上获得了一些帮助,并定义了/name等ID属性。经过一些工作,我得到了我的单页网站,以使用自定义ID。但!这是我的问题:

当访问者第一次访问索引文件时,我希望浏览器像这样重定向:

example.com - > 1500后重定向 - > example.com /#/所有/

但是,由于它是一个单页网站,所有其他div(在/all/处于活动状态时隐藏)都位于同一页面上,因此具有相同的重定向属性。因此,如果用户直接访问ID(example.com/#/work/),浏览器会在1500之后重定向到#/all/。因此无法直接链接到某些项目。另外,我有多个以/work/开头的ID。

这就是我现在正在使用的

window.setTimeout(function() {

window.location.href = '#/all/';    // redirect the visitor when loading the index for the first time
}, 1500);    
$('#/work/...','#/about/').on('ready', function () { // Select ALL that has the id "#/about/", and/or all that starts with the id "#/work/"
    window.location = ""; // No redirect, just load the page with the targeted id active
});

1 个答案:

答案 0 :(得分:1)

只有在没有定义哈希的情况下才运行重定向。

if (window.location.hash.length === 0){
    window.setTimeout(function() {
        window.location.hash = '/all/';    // redirect the visitor when loading the index for the first time
    }, 1500);  
}
相关问题