Facebook 喜欢使用 #!在通过ajax加载内容的url中

发布于 2024-09-27 20:39:41 字数 852 浏览 0 评论 0原文

我正在使用 ajax 加载我的网站的内容。 当我动态加载页面http://www.example.com/a/b/c时 我将当前页面的网址更改为 http//www.example.com/xyz/#!/a/b/c 通过使用 window.location 通过 javascript 设置它

现在我想做的是,当有人输入网址 http/ /www.example.com/xyz/#!/a/b/c 在浏览器中,他应该被定向到 http://www.example.com/a/b/c 而不是 http://www.example.com /xyz/

这与 facebook 上使用的类似 http://www.facebook.com/?ref=logo#!/reqs.php#friend 自动带我去 http://www.facebook.com/reqs.php#friend 当没有ajax加载时。

我尝试使用 javascript 来实现这个,

    requestedURL = ""+window.location+"";
    position = requestedURL.search("#!");
    domainame = "http://localhost"
    if(position != -1){
        newpage = requestedURL.substr(position+2);
        requestedURL = "";
        window.location = domainame+newpage;
    }

这确实对我有用,但是现在每次当我通过 ajax 加载页面时,这个脚本都会被执行。

有人可以帮我解决这个问题吗? 通过 .htaccess 的解决方案也适合我。

I am using ajax to load content for my site.
When I dynamically load a page http//www.example.com/a/b/c
I change my current page's url to http//www.example.com/xyz/#!/a/b/c by setting it through javascript using window.location

Now what I want to do is that when somebody enters a url http//www.example.com/xyz/#!/a/b/c in the browser, he should be directed to http//www.example.com/a/b/c instead of http//www.example.com/xyz/

This is similar to what is used on facebook
http//www.facebook.com/?ref=logo#!/reqs.php#friend
automatically takes me to
http//www.facebook.com/reqs.php#friend
when loaded without ajax.

I tried using javascript to implement this

    requestedURL = ""+window.location+"";
    position = requestedURL.search("#!");
    domainame = "http://localhost"
    if(position != -1){
        newpage = requestedURL.substr(position+2);
        requestedURL = "";
        window.location = domainame+newpage;
    }

This did work me but now every time when I load a page even through ajax, this script gets executed.

Can someone help me out with this.
A solution through .htaccess would also do for me.

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

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

发布评论

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

评论(1

在你怀里撒娇 2024-10-04 20:39:41

看起来我会尝试从应用程序的路由模块(在服务器端)而不是通过 Ajax 进行模拟。

It looks like something I would try to emulate from the routing module of my application (on the server side) instead of via Ajax.

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