1

I'm trying to detect a URL change but the URL has a dynamic value after the hash, for example: http://myurl/#!/contact/179593927.

I was able to make the below work but on http://myurl/#!/contact/ ONLY, but not on http://myurl/#!/contact/123456789.

$(window).on('hashchange', function(e){
 if (location.hash === '#!/contact/') {
    $('.controls-bar-1').appendTo('.contact-mod');
    $('.controls-bar-2').appendTo('.contact-mod');
  }
});

Please point me to the right direction.

Rory McCrossan
  • 306,214
  • 37
  • 269
  • 303

1 Answers1

0

The issue is because you're checking the exact value of the URL fragment. If you want to check if the /contact part exists regardless of whether a value is provided or not you could use indexOf() to check the start of the fragment only, like this:

if (location.hash.indexOf('#!/contact/') === 0) {
  $('.controls-bar-1, .controls-bar-2').appendTo('.contact-mod');
}
Rory McCrossan
  • 306,214
  • 37
  • 269
  • 303