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

标签 javascript jquery redirect

我刚刚在 stackoverflow 上获得了一些关于定义 ID 属性(例如 /name)的帮助。经过一番工作后,我的单页网站可以使用自定义 ID。但!这是我的问题:

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

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

但是,由于它是一个单页网站,因此我的所有其他 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
});

最佳答案

仅当尚未定义哈希时才运行重定向..

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);  
}

关于javascript - 从页面重定向中排除 id (jquery),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27260118/

相关文章:

jquery - 定位DIV

javascript - 检查另一个单选按钮字段时如何强制单选按钮保持选中状态?

google-chrome - 如何在不触发浏览器 SSL 检查的情况下重定向 www 流量?

apache - 我可以使用 .htaccess 重定向到目录中的最新文件吗?

javascript - 如何让事件通过可见的DOM元素

javascript - 在这种情况下 setState 怎么不起作用?

javascript - 如何识别onbeforeunload是点击关闭按钮引起的

Javascript - 将图像添加到随机位置的节点

jquery 检查两个复选框取消选中现有的选中复选框

redirect - 在 UNIX 中什么是 "cat file1 > file1 does?"