Ajax - 维护 url 中的状态 - 否 #

发布于 2024-09-25 04:37:43 字数 722 浏览 0 评论 0原文

我正在开发一个 AJAX 网站,其中有两个搜索参数。我做了一些 mod-rewrite 并检查 $_GET 变量,以便我可以做类似的事情..

site.com/var1/var2/ ->根据参数自动进行搜索。

现在我想要的是手动进行搜索的人能够获得该格式的网址。我能找到的唯一方法是使用..

location.hash = 'foo';

修改url。这将使其类似于.. site.com/#var1,

这不如 mod 重写那么好。我发现有效的是,如果在我的搜索功能中进行ajax调用,我有这段代码,

     // avoid appending further variables if there are already variables
     if(location.href == 'some absolute website path')
        location.href = var1+'/'+var2+'/';

这将有效,但基本上会强制页面加载,然后我的自动搜索php/javascript将由于mod重写而启动。所以这可行,但是它涉及我宁愿避免的额外页面刷新。

还有更好的解决方案吗?理想情况下,如果我能够使用 location.href ,一旦我更改值,它不会导致页面加载,而只是更改 url 将是理想的(同时维护我的 mod-rewrite 链接,没有 #标记)。

我正在使用 jquery 和 php。

I am working on an AJAX website where there are two search parameters. I did some mod-rewrite and checking for $_GET variables so that i can do something like..

site.com/var1/var2/ -> automatically do the search based on the parameters.

Now what I want is for people who do the search manually, to be able to have the url in that format. The only method that I've been able to find has to do w/modifying the url using..

location.hash = 'foo';

which would make it something like.. site.com/#var1

Which isn't as nice as the mod-rewrite. What I have found that works is if in my search function that does the ajax call i have this code

     // avoid appending further variables if there are already variables
     if(location.href == 'some absolute website path')
        location.href = var1+'/'+var2+'/';

This will work, but basically forces the page load and then my auto search php/javascript will kick in due to the mod-rewrite. SO this works, however it involves an extra page refresh that I would rather avoid.

Any better solutions out there? Ideally if i was able to use location.href where it didn't cause the page to load once i change the value, but would just change in the url would be ideal (while maintaining my mod-rewrite links, w/out the # marks).

I am using jquery and php.

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

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

发布评论

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

评论(1

撧情箌佬 2024-10-02 04:37:43

设计就是这样,您无法在不引发新请求的情况下更改 url 或路径。问候。

It's that way by design, there is no way yo change the url or path without causing a new request. Regards.

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