js 获取 window.location.hash 并将其拆分为数组并替换某些数组元素

发布于 2024-10-17 22:05:44 字数 2328 浏览 7 评论 0原文

嘿,非常聪明的人!

我对此感到困惑,并尝试了一周,但找不到合理的解决方案。

基本上,每当用户单击两组不同的链接时,我都希望附加一个准备好的网址。

到目前为止,在我的代码中发生这种情况

$(selecter a.1).live('click', function(){
   window.location.hash = '!/' + usr + '/' + $(this).attr('href').replace('.php', '/');
}

将使网址看起来像: http://www. site.com/!#/username/page1/

然后我调用一个函数来检测 url 中的哈希更改并将 url 返回到它应该的样子:

$(window).bind('hashchange', function(){
            hash = window.location.hash;
            //if( hash.indexOf( "/files/" ) == 6 )
                //newHash = window.location.hash.substring(3).replace(usr + '/files', '').replace('/', '.php');
            //else
                newHash = window.location.hash.substring(3).replace(usr + '/', '').replace('/', '.php');
            //newHash = window.location.hash.replace(dump, origHash, 'g');
            console.log(newHash);
            if (newHash) {
                $wrap.find("#main-content").fadeOut(200, function() {
                    $wrap.load(newHash + " #main-content", function() {
                        $content.fadeIn(function() {
                            $wrap.animate({
                                height: baseHeight + $content.height() + "px"
                            }, 500);
                        });
                    });
                });
            }
    });

那里没有问题,问题是当用户单击我想以稍微不同的方式附加到网址的第二组链接。

$(selecter a.2).live('click', function(){
       window.location.hash = '!/' + usr + '/' + $(this).attr('href').replace('.php', '/');
}

看起来像这样: http://www.site.com/!# /username/files/sub-page/

链接的 href 看起来像这样 href="files/sub-page.php"

我遇到的问题是,将窗口绑定到 hashchange 时,它​​将尝试准备url 返回到其原始“状态”,该状态仅适用于第一组链接。

我的想法是为什么不直接将原始 href 值替换为 window.location.hash 但后来意识到......如果用户重新加载页面怎么办?嗯嗯!原始哈希值将会丢失!并且 url 依赖于哈希更改的检测,以便它可以加载正确的 php 页面。

好吧,接下来我想也许我可以以某种方式切掉除了网址中最后一个单词之外的所有内容,并执行替换('/','.php')来添加扩展名......

好吧,其他问题是,在 url 中,它指向位于 files/ 目录中的 files/sub-page.php。

我最初将所有内容都放在一个目录中,并想手动将 files/ 附加到 url,但遇到了相同逻辑的问题,所以我想我将它们移动到一个名为 files/ 的文件夹中,并找出如何欺骗 js 离开那里的目录路径...

好吧,这就是我的故事...

我可以寻求帮助吗?

Hey extremely smart people!

I have been boggled with this and have tried for a week and can't find a logical solution.

Basically whenever a user clicks on either two different sets of links I want a prepared url to be appended.

So far in my code this happens

$(selecter a.1).live('click', function(){
   window.location.hash = '!/' + usr + '/' + $(this).attr('href').replace('.php', '/');
}

this will make the url look like: http://www.site.com/!#/username/page1/

then I call a function to detect hash change in the url and return the url back to what it should be:

$(window).bind('hashchange', function(){
            hash = window.location.hash;
            //if( hash.indexOf( "/files/" ) == 6 )
                //newHash = window.location.hash.substring(3).replace(usr + '/files', '').replace('/', '.php');
            //else
                newHash = window.location.hash.substring(3).replace(usr + '/', '').replace('/', '.php');
            //newHash = window.location.hash.replace(dump, origHash, 'g');
            console.log(newHash);
            if (newHash) {
                $wrap.find("#main-content").fadeOut(200, function() {
                    $wrap.load(newHash + " #main-content", function() {
                        $content.fadeIn(function() {
                            $wrap.animate({
                                height: baseHeight + $content.height() + "px"
                            }, 500);
                        });
                    });
                });
            }
    });

no problems there, the problem is when the user click on the second set of links that i want to append a little differently to the url.

$(selecter a.2).live('click', function(){
       window.location.hash = '!/' + usr + '/' + $(this).attr('href').replace('.php', '/');
}

will look like this: http://www.site.com/!#/username/files/sub-page/

the href of the links look like this href="files/sub-page.php"

the problem I have is that with binding the window to a hashchange it will attempt to prepare the url back to its original 'state' which is only applicable to the first set of links.

I had the idea of why not just replace the orginal href value to the window.location.hash but then realized that well... what if the user reloads the page?? Well duh! the original hash will be lost! and the url is dependant in the detection of the hash change so that it can load the proper php page.

Ok, so next I thought maybe I can somehow slice out everything except the last word in the in the url and do a replace('/', '.php') to get the extension tacked on...

Ok, well the other issue with that is that in the url its pointing to files/sub-page.php which is located in a files/ directory.

I originally had everything in one directory and wanted to manually append the files/ to the url but was running in problems with the same logic, so I thought I move them into a folder called files/ and figure out how to trick the js into leaving that dir path in there...

Ok, thats my story...

can I please get some help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终陌 2024-10-24 22:05:44

你很幸运: https://github.com/allmarkedup/jQuery-URL-Parser

为您解析 url,并为您提供有关如何访问 url 的每一位的帮助方法!

You're in luck: https://github.com/allmarkedup/jQuery-URL-Parser

parses the url for you and provides you with helper methods on how to access each bit of the url!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文