2016-11-06 110 views
2

我一直在试图建立重定向的范围开始从/店/店#ecwid网址!/〜/导致/店#!/〜/车JS 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行为有关,但无法弄清楚如何与之对抗。

需要帮助?

回答

1

维塔利来自Ecwid这里。

脚本当前版本中的问题是,它没有检测到您所描述的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> 
+0

这很好,谢谢! – tonydoe