3

I'm building a live search + filter method, and I've noticed when I use that method (it's an example) that hash after it shows in the URL - it is immediately being removed. If I move that hash set to e.g. button's click method it works fine. Why ?

$('.menu-link').click(function () {            
        window.location.hash = 'search-container';
});
Tony
  • 12,405
  • 36
  • 126
  • 226

2 Answers2

11

It's disappearing because your link is being followed, causing the location to be changed to the href of the a. If you add a return false; to the end of your click handler, then the location won't change.

Jacob
  • 77,566
  • 24
  • 149
  • 228
  • @Jacob ... THANK YOU. THANK YOU. THANK YOU. I was losing my mind trying to figure this out as well. – carrabino Oct 31 '13 at 17:53
  • This behavior seems completely inconsistent between browsers. I didn't have a return for a hybrid mobile app, and it works for iOS 6/7, and Android 2.3 - 4.3. On 4.4, it caused problems, unless you called the evaluateJavascript function instead of loadUrl. Very strange. – shortstuffsushi May 05 '14 at 21:52
3

Try this:

$('.menu-link').click(function (event) {     
    event.preventDefault();
    window.location.hash = 'search-container';
});
Burntime
  • 2,334
  • 2
  • 15
  • 20