tonydoe tonydoe - 28 days ago 7
Ajax Question

js ecwid url redirect

I have been trying to set up redirects for a range of ecwid urls starting from /shop to /shop#!/~/ to lead to /shop#!/~/cart

I have come up with this code:



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





It works all right but there is a problem. When I am at /shop#!/~/cart and then manually change url to, say, /shop#!/~/ it won't get redirected until I refresh the page. I believe this has something to do with ajax behavior of ecwid shopping cart but can't figure out how to fight it.

Need help?

Answer

Vitaly from Ecwid here.

The issue in the current version of your script is that it doesn't detect the changes to the URL like you described.

So you will need to create a handler for such situations separately. For example, you can do it like this:

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