Ajax - 在url中维护状态 - 没有#

时间:2022-03-13 03:54:45

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..

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

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

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

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';

location.hash ='foo';

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

这将使它像.. 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

哪个不如mod-rewrite好。我发现有效的方法是在我的搜索函数中执行ajax调用我有这个代码

     // 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.

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

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).

还有更好的解决方案吗?理想情况下,如果我能够使用location.href,一旦我更改了值,它不会导致页面加载,但只是更改网址将是理想的(同时保持我的mod-rewrite链接,w / out#分数)。

I am using jquery and php.

我正在使用jquery和php。

1 个解决方案

#1


0  

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

这就是设计方式,没有办法在不引起新请求的情况下更改URL或路径。问候。

#1


0  

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

这就是设计方式,没有办法在不引起新请求的情况下更改URL或路径。问候。