【问题标题】:js ecwid url redirectjs ecwid url 重定向
【发布时间】:2016-11-06 07:18:19
【问题描述】:
我一直在尝试为从 /shop 到 /shop#!/~/ 到 /shop#!/~/cart 的一系列 ecwid url 设置重定向
我想出了这个代码:
var wrong_url = ['http://example.com/shop','http://example.com/shop#','http://example.com/shop#!','http://example.com/shop#!/','http://example.com/shop#!/~','http://example.com/shop#!/~/'];
var current_url = document.URL;
for (i=0;i<wrong_url.length;i++) {
if (current_url==wrong_url[i]) {
document.location.replace('http://example.com/shop#!/~/cart');
break;
}
}
一切正常,但有问题。当我在 /shop#!/~/cart 然后手动将 url 更改为 /shop#!/~/ 时,它不会被重定向,直到我刷新页面。我认为这与 ecwid 购物车的 ajax 行为有关,但不知道如何解决。
需要帮助吗?
【问题讨论】:
标签:
javascript
ajax
redirect
【解决方案1】:
这里是来自 Ecwid 的 Vitaly。
当前版本的脚本中的问题是它没有像您描述的那样检测对 URL 的更改。
因此,您需要为此类情况单独创建一个处理程序。例如,您可以这样做:
<script>
var wrong_url = ['http://example.com/shop','http://example.com/shop#','http://example.com/shop#!','http://example.com/shop#!/','http://example.com/shop#!/~','http://example.com/shop#!/~/'];
var current_url = document.URL;
// function to check current URL and make a redirect
function checkUrl(){
for (i=0;i<wrong_url.length;i++) {
if (current_url==wrong_url[i]) {
document.location.replace('http://example.com/shop#!/~/cart');
break;
}
}
}
// Execute checkUrl() function each time URL is changed
$(window).bind('hashchange', function() {
checkUrl();
});
// Execute checkUrl() function on initial page load
checkUrl();
</script>