1

I have an ajax function that performs a filter and then changes the url.

How would I do the equivalent of the following:

$(window.location).change(function() {
    alert('hello');
})
David542
  • 104,438
  • 178
  • 489
  • 842
  • So when the user navigates away from the page? – asafreedman Jun 23 '13 at 23:39
  • No, the user is still on the page. The querystring is just changing. For example: /tasks/?task_status=problem --> /tasks/?task_status=problem&definition=mastering – David542 Jun 23 '13 at 23:40
  • More accurately, when `window.location.search` changes. – David542 Jun 23 '13 at 23:41
  • I'd say the best you can do is working with timers for checking every fix intervals – Claudio Redi Jun 23 '13 at 23:43
  • Duplicate? http://stackoverflow.com/questions/3522090/event-when-window-location-href-changes – dennythecoder Jun 23 '13 at 23:43
  • @David542 Are you using [`history.pushState()`](https://developer.mozilla.org/en-US/docs/Web/Guide/DOM/Manipulating_the_browser_history#Adding_and_modifying_history_entries) or just setting `window.location` directly? You may want the [`popstate` event](https://developer.mozilla.org/en-US/docs/Web/API/window.onpopstate). – Jonathan Lonowski Jun 23 '13 at 23:44
  • Have a look here http://addyosmani.com/resources/essentialjsdesignpatterns/book/#observerpatternjavascript – elclanrs Jun 23 '13 at 23:45

2 Answers2

0

You would probably have a look at the history API. You can use the onhashchange event to detect that change.

$(window).on('hashchange', function() {
    alert('hello');
});
Mimo
  • 6,015
  • 6
  • 36
  • 46
0

If you change location, browser stop all of script and don't trigger change function, you can use only unload event of document when use change the location, check it here http://api.jquery.com/unload/ and if you only change bookmark then only handle hashchange event, you can find sample jquery plugin here http://benalman.com/projects/jquery-hashchange-plugin/..

Mehdi Yeganeh
  • 2,019
  • 2
  • 24
  • 42