mtrpcic / pathjs

Simple, lightweight routing for web browsers
1.1k stars 178 forks source link

Capture any URL change #77

Open amatiasq opened 10 years ago

amatiasq commented 10 years ago

Hey there, I need some way to capture any type of URL change, is there a way to do this? Something like

Path.onNavigate(function(route) {
  model.data = {};
});
nits commented 9 years ago

Did you find solution for this?

amatiasq commented 9 years ago

No, I moved to another library.

nits commented 9 years ago

Which library?

amatiasq commented 9 years ago

Sorry I don't remember, it's more than one year ago.

nits commented 9 years ago

Ok. Path.rescue did the work for me. Thanks anyways.

ghost commented 8 years ago

@amatiasq The way I do it in pure JS is:

var routeFunc = function() {
  var page = document.location.hash.split('=');
  switch (page[0]) {
    case '#!archive':
      // some code
      break;
    case '#!home':
      // some code
      break;
    default:
      // some code
  }
};

var initFunc = function() {
  routeFunc();
  window.addEventListener('hashchange', routeFunc, false);
};
window.addEventListener('load', initFunc, false);

You'll have to prefix all urls with #! like in the following example: #!post=test_page

I'm using the same code in my blog - https://github.com/wifiextender/wifiextender.github.io/blob/master/dev/src/main.js#L105