简体   繁体   English

不带斜杠的History.js URL无法正常工作

[英]History.js URL without trailing slash doesn't work correctly

I'm using the History.js for URL changes without reloading. 我正在使用History.js进行URL更改,而无需重新加载。 This is used on a website with product filters, where the filters are placed in the URL so the URL can be shared with the active filters. 这在带有产品过滤器的网站上使用,该产品将过滤器放置在URL中,以便可以与活动过滤器共享URL。

The URL's of the website don't end on a trailing slash, so a valid URL for the site is: www.mysite.com/products . 网站的URL不能以斜杠结尾,因此该网站的有效URL为: www.mysite.com/products

This URL would show you a list with all products an you are ably to apply filters. 该URL将向您显示所有产品列表,您可以应用过滤器。 Applying a filters should change the URL to: 应用过滤器应将URL更改为:

www.mysite.com/products/?filter1=2,4,6&filter2=4

This works in FireFox, Chrome, IE11. 这适用于FireFox,Chrome,IE11。 In IE9 however, it fails on the trailing slash. 但是,在IE9中,它不能以斜杠结尾。 The URL in IE9 is change to: IE9中的URL更改为:

www.mysite.com/products#products?filter1=2,4,6&filter2=4

It seems that it sees product as a parameter, in stead of a part of the URL. 似乎将product视为参数,而不是URL的一部分。 If I manually add a trailing slash behind the URL, it works fine in IE9. 如果在URL后面手动添加斜杠,则在IE9中可以正常工作。

Base URL: www.mysite.com/products/ will be www.mysite.com/products/?filter1=2,4,6&filter2=4 when using the history.js, even in IE9. 使用history.js时,即使在IE9中,基本URL: www.mysite.com/products/也会是www.mysite.com/products/?filter1=2,4,6&filter2=4

So summary: Adding a trailing slash at the end of the base URL fixes the problem. 总结:在基本URL的末尾添加斜杠可解决此问题。 But I don't want to add that last trailing slash, the history.js should do that. 但是我不想添加最后一个斜杠,history.js应该这样做。 How would that be accomplished? 那将如何实现?

EDIT 编辑

I think the problem is in this part of the code, th 我认为问题出在代码的这一部分

var basePageUrl = (History.getLocationHref()).replace(/[#\?].*/,'').replace(/[^\/]+$/,function(part,index,string){
  return (/[^\/]$/).test(part) ? '' : part;
}).replace(/\/+$/,'')+'/';

This script turns this baseURL: www.mysite.com/products into: www.mysite.com 该脚本将此baseURL: www.mysite.com/products转换为: www.mysite.com

I'm not that strong in regex, so actually have no idea what the regex does in this example. 我对正则表达式的了解并不强,因此实际上不知道此示例中正则表达式的功能。

in the History.getPageUrl function, I've added this line 在History.getPageUrl函数中,我添加了这一行

//Yoni's hack to not have trailing slash
        if (pageUrl.substr(pageUrl.length - 1, 1) == '/') {
            pageUrl = pageUrl.substr(0, pageUrl.length - 1);
        }

just before the 就在

return pageUrl;

In Symfony 2 we are not using trailing slash, so there is never use of it, and it's then a good condition, if you need both possibilities, you have to handle it in another way probably 在Symfony 2中,我们不使用尾部斜杠,因此永远不会使用它,这是一个很好的条件,如果您同时需要两种可能,则可能必须以其他方式处理它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM